WCF如何根据请求决定何时返回SOAP或JSON? [英] How WCF decides when return SOAP or JSON depending on the request?

查看:82
本文介绍了WCF如何根据请求决定何时返回SOAP或JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚创建了第一个WCF REST服务。我希望它返回JSON,所以我指定了响应格式。起初,我很沮丧,因为它一直在返回SOAP,我不知道为什么,但我看到一个博主正在使用Fiddler,所以我尝试了它,然后我得到了一个JSON响应。

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.

我认为原因是因为Fiddler没有发送此HTTP标头:

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

但它没有用。 WCF如何决定何时使用JSON或SOAP?

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

有没有办法强制总是返回JSON?

Is there a way to force to return JSON always?

我想确保当我使用该服务时,它将返回JSON而不是SOAP。

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

谢谢。

更新:示例代码:

[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" };
    }
}


推荐答案

我有这样的端点行为

<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.

我还不完全确定的一件事是关于WCF似乎产生的两种不同的Json样式。如果使用enableWebScript,这将产生{d:{...}} Json格式,defaultOutgoingResponseFormat选项设置为Json,不会有d-object,只有Json。

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.

这篇关于WCF如何根据请求决定何时返回SOAP或JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆