开发者

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

开发者 https://www.devze.com 2023-04-05 20:04 出处:网络
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,

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
        开发者_JAVA技巧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" };
    }
}


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.


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)]


Have you tried setting your service configuration to use webHttpBinding?

Expose webHttpBinding endpoint in a WCF service

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号