0

I am newbie to C# and WCF.

I have already gone through this link. But cannot solve.

Following is just a sample GET service method.

Expected JSON format is

{"result":"Hello","add":4}

Result JSON format is

{"GetDataResult":{"add":4,"result":"Hello"}}

Entity class is

[ServiceContract]
public interface IConnectDBService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
   BodyStyle = WebMessageBodyStyle.Wrapped,
   UriTemplate = "val",
   ResponseFormat = WebMessageFormat.Json)]
    MyObject GetData();
}

[DataContract]
public class MyObject
{

    [DataMember(Name = "result")]
    public string result { get; set; }
    [DataMember(Name = "add")]
    public int add { get; set; }

}

Service class is

public MyObject GetData()
    {
        return new MyObject { result = "Hello", add = 4 };
    }

How do I eliminate "GetDataResult" from result. And notice that key's are sorted alphabetically in the resulted JSON. Any suggestion to format JSON in our vision.

Thank You

Community
  • 1
  • 1
Aswin
  • 1,154
  • 1
  • 11
  • 29

1 Answers1

1

The extra GetDataResult is because of BodyStyle = WebMessageBodyStyle.Wrapped. You can either remove the property or change it to WebMessageBodyStyle.Bare.

YK1
  • 7,327
  • 1
  • 21
  • 28
  • Yes, same for POST, however, in that case, you also have to take care of JSON in the POST request. See the options of `WebMessageBodyStyle` enumeration on MSDN. – YK1 Nov 26 '13 at 08:38
  • 'Take care'..What it does mean? My post service respond only with `Wrapped` but with unexpected extra key. When changed to `Bare` it returns XML/HTML with 'Server encountered an error processing. See server log' message. Any suggestions regarding this? – Aswin Nov 26 '13 at 08:55
  • By 'take care' i meant you have options in the enumeration to set either request or response or both or neither to be wrapped. For your error you may have to show us what data you are posting. Either modify your question if closely related, or open new question if it will change the whole meaning of the question. – YK1 Nov 26 '13 at 17:27