0

I have looked and around and seen that it is possible to return objects serialized into Json from a WCF web service. Does anyone know how I can do this?

Thanks

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
Funky
  • 12,890
  • 35
  • 106
  • 161
  • Make sure your requesting client is sending the "Accept: application/json" header? – Randy James Jul 15 '13 at 20:55
  • You can see bellow link : [return-clean-json-from-a-wcf-service][1] [1]: http://stackoverflow.com/questions/2086666/how-do-i-return-clean-json-from-a-wcf-service – M.Mohammadi Jun 19 '15 at 23:07

2 Answers2

1

You will have to add attribute to the service like this

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    ObjectName YourMethodName();
Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

Yes it is possible you can set automaticFormatSelectionEnabled to true standardEndpoint of webHttpEndpoint in web.config like

<webHttpEndpoint>
  <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>

And you need to add http header for json response to your client

using (HttpClient client = new HttpClient("endpoint"))
{
     HttpRequestMessage request = new HttpRequestMessage("GET", "SomeMethod");                
     request.Headers.Accept.AddString("application/json");
    ...
}
Gökhan Girgin
  • 1,164
  • 8
  • 12