Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
Make sure your requesting client is sending the "Accept: application/json" header? – Randy James yesterday
examples? tutorials? – Funky 21 hours ago

3 Answers

up vote 1 down vote accepted

You will have to add attribute to the service like this

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    ObjectName YourMethodName();
share|improve this answer
This alone does not work – Funky 20 hours ago
@Funky This will return Object serialized as JSON. Can you please elaborate a bit ? – Ehsan Ullah 20 hours ago

you can do with marking your operation contract with WebGet attribute and its RequestBody, ResponseBody format.

You will have to make serialization and deserialization at client side.

Here's a article which describes how to make your operations for JSON enabled and de/serialization at client side

share|improve this answer

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");
    ...
}
share|improve this answer
Can you provide a tutorial for this or link? – Funky 20 hours ago
have a look at msdn – Gökhan Girgin 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.