How WCF decides when return SOAP or JSON depending on the request?
Asked Answered
W

3

2

I just created my first WCF REST service. I wanted it to return JSON, so i especified the response format. At first, I was frustrated because it was returning SOAP all the time and I didn't know why, but I saw that a blogger was using Fiddler, so I tried with it and then I got a JSON response.

I think that the reason is because Fiddler is not sending this HTTP header:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

But then I tried to craft a request using this header:

Accept: application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

But it didn't work. How is WCF deciding when to use JSON or SOAP?

Is there a way to force to return JSON always?

I would like to be sure that when I consume the service, it will return JSON and not SOAP.

Thanks.

Update: Sample code:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
    [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json, RequestFormat= WebMessageFormat.Json)]
    public List<SampleItem> GetCollection()
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
    }

    [WebInvoke(UriTemplate = "", Method = "POST")]
    public SampleItem Create(SampleItem instance)
    {
        // TODO: Add the new instance of SampleItem to the collection
        throw new NotImplementedException();
    }

    [WebGet(UriTemplate = "{id}", ResponseFormat = WebMessageFormat.Json)]
    public SampleItem Get(string id)
    {
        // TODO: Return the instance of SampleItem with the given id
        return new SampleItem() { Id = Int32.Parse(id), StringValue = "Hello" };
    }
}
Wrecker answered 20/9, 2011 at 0:28 Comment(1)
You cannot return SOAP - SOAP is a calling protocol - not a data format (like JSON or XML)Horseradish
M
2

I have an endpoint behavior like this

<endpointBehaviors>
  <behavior name="jsonEndpoint">
    <webHttp defaultOutgoingResponseFormat="Json" />
  </behavior>
</endpointBehaviors>

If you configure this as an endpoint behavior you don't need to touch your operation contracts and they can produce different output if they're accessed over a different endpoint.

The one thing I'm not yet completely sure is about the two different Json styles that WCF seems to produce. If you use enableWebScript this will produce the { "d" : { ... } } Json format, with the defaultOutgoingResponseFormat option set to Json there will be no d-object, but just Json.

Mawson answered 20/9, 2011 at 9:0 Comment(0)
A
1

I need to look at your code to understand what exactly you are doing. But a quick check here. Have you applied following attributes to your service definition?

[System.ServiceModel.Web.WebGet(ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]

Agustinaah answered 20/9, 2011 at 2:30 Comment(1)
I have updated the post. It is the sample code that comes with the WCF REST template, nothing else :pWrecker
B
0

Have you tried setting your service configuration to use webHttpBinding?

Expose webHttpBinding endpoint in a WCF service

Billet answered 13/2, 2013 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.