协商媒体类型时,WCF REST不返回Vary响应头 [英] WCF REST is not returning a Vary response header when media type is negotiated

查看:157
本文介绍了协商媒体类型时,WCF REST不返回Vary响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF REST服务:

I have a simple WCF REST service:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
    [WebGet(UriTemplate = "{id}")]
    public SampleItem Get(string id)
    {
        return new SampleItem() { Id = Int32.Parse(id), StringValue = "Hello" };
    }
}

服务应该对媒体没有限制返回。

There is not constrain about the media that the service should return.

当我发送指定 json 格式的请求时,它返回JSON:

When I send a request specifying json format, it returns JSON:

GET http://localhost/RestService/4 HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Host: localhost

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 30
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 02 Oct 2011 18:06:47 GMT

{"Id":4,"StringValue":"Hello"}

当我指定 xml 时,它返回XML:

When I specify xml, it returns XML:

GET http://localhost/RestService/4 HTTP/1.1
User-Agent: Fiddler
Accept: application/xml
Host: localhost

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 194
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 02 Oct 2011 18:06:35 GMT

<SampleItem xmlns="http://schemas.datacontract.org/2004/07/RestPrototype.Service" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>4</Id><StringValue>Hello</StringValue></SampleItem>

到目前为止,问题是该服务没有返回改变 HTTP标头,说明内容已经协商,接受 http标头是一个决定性因素。

So far so good, the problem is that the service doesn't return a Vary HTTP header to say that the content has been negotiated and that the Accept http header has been a determinant factor.

不应该是这样吗?:

GET http://localhost/RestService/4 HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Host: localhost

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 30
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Vary:Accept
Date: Sun, 02 Oct 2011 18:06:47 GMT

{"Id":4,"StringValue":"Hello"}

据我所知,在缓存方面,Vary标头会告诉中间缓存,响应是根据URI和接受 HTTP标头。否则,代理可以缓存json响应,并将其用于要求xml的人。

As far as I know, in terms of caching, the "Vary" header will tell intermediate caches that the response is generated based on the URI and the Accept HTTP header. Otherwise, a proxy could cache a json response, and use it for somebody that is asking xml.

有什么方法可以让WCF REST自动放置此标头?

There is any way to make WCF REST put this header automatically?

谢谢。

推荐答案

在WCF Web API中,我们计划添加在连接期间自动设置Vary标头。目前,如果您使用的是Web API,则可以使用自定义操作处理程序或消息处理程序来完成此操作。对于WCF HTTP,然后使用消息检查器作为Carlos推荐的方法。

In WCF Web API, we are planning to add automatically setting the Vary header during conneg. For now if you are using Web API, you can do this by either using a custom operation handler or message handler. For WCF HTTP then using a message inspector as Carlos recommended is the way to go.

这篇关于协商媒体类型时,WCF REST不返回Vary响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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