WCF禁用分块传输编码 [英] WCF disable chunked Transfer-Encoding
问题描述
我使用了一个宁静的WCF,并且我调用了一个具有流返回类型的方法,这就是响应:
I use a restful WCF, and i call a method which has got a stream return type and this is the Response:
HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/octet-stream
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 08 Nov 2013 11:25:29 GMT
6
1;1;
0
我想禁用
Transfer-Encoding:chunked
Transfer-Encoding: chunked
因为它写了一些我不需要的信息(体内6和0)
Because it write some plus information what i don't need (6 and 0 in the body)
我该怎么做?
推荐答案
创建WCF端点时,需要指定传输模式( TransferMode
。
When you create a WCF endpoint you need to specify what transfer mode (TransferMode
) you are using.
- 如果设置为
StreamedResponse
,Transfer-Encoding:chunked
将自动添加到响应标头中,Content-Length
将被自动省略,即使你设置了,也无法对此做任何事情。明确Content-Length
。它是内容长度或传输编码,它们根据传输模式互相替换,请阅读这(第一个)段落)。 - 如果设置为
缓冲
,Transfer-Encoding:chunked
将不会自动添加,如果您指定,将允许包含Content-Length
。
- If it is set to
StreamedResponse
,Transfer-Encoding: chunked
will be added automatically to the response headers,Content-Length
will be automatically omitted and there is nothing you can do about that, even if you set theContent-Length
explicitly. It is EITHER Content-Length or Transfer-Encoding, they replace each other depending on transfer mode, read this (the first paragraph). - If it is set to
Buffered
,Transfer-Encoding: chunked
will be not be added automatically, which will allowContent-Length
to be included, if you specify it.
所以像这样创建你的端点:
So create your endpoint like this:
WebHttpBinding wb = new WebHttpBinding(WebHttpSecurityMode.Transport);
wb.TransferMode = TransferMode.Buffered; // TransferMode.StreamedResponse;
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(myService.IBlah), blah, "");
当我从WCF WebGet流式传输MP3音频时遇到此问题我看不到内容长度。一旦您的端点配置了缓冲
传输模式,您将开始看到 Content-Length
。
I had this problem when I was streaming an MP3 audio from a WCF WebGet and I couldn't see Content-Length. Once your endpoint is configured with Buffered
transfer mode, you will start seeing Content-Length
.
对我而言,这只是战斗的一半。为了在Chrome上正常播放MP3,我还必须在响应标头中添加 Accept-Ranges:bytes
,以便可以在Chrome上搜索音频。
For me this was only half the battle. In order to properly play MP3 on Chrome, I had to also add Accept-Ranges: bytes
to the response headers so that the audio was seekable on Chrome.
这篇关于WCF禁用分块传输编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!