C# 如何在 WCF 4.5 中使用 gzip 压缩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15680087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get gzip compression working in WCF 4.5
提问by NickD
WCF 4.5 supports GZIP without third party libraries or handwritten extensions. I got it working via TCP Binding, but cannot find a way to get it working via HTTP Binding. my wcf - Service is self hosted in a windows service.
WCF 4.5 支持 GZIP,无需第三方库或手写扩展。我通过 TCP 绑定让它工作,但找不到通过 HTTP 绑定让它工作的方法。我的 wcf - 服务自托管在 Windows 服务中。
Addon: i am not allowed to use IIS; i can't switch to any WCF replacement.
插件:我不允许使用 IIS;我无法切换到任何 WCF 替代品。
this works with gzip:
这适用于 gzip:
binding="customBinding" bindingConfiguration="tcpCompressionBinding" name="tcp"
and this is what i currently use for http:
这就是我目前用于 http 的内容:
binding="basicHttpBinding" bindingConfiguration="httpBinding" name="http"
The documentation does not really help me: http://msdn.microsoft.com/en-us/library/dd456789.aspx.
该文档并没有真正帮助我:http: //msdn.microsoft.com/en-us/library/dd456789.aspx。
But, according to this it should work:
但是,根据this它应该工作:
Beginning with WCF 4.5 the WCF binary encoder adds support for compression. The type of compression is configured with the CompressionFormat property. Both the client and the service must configure the CompressionFormat property. Compression will work for HTTP, HTTPS, and TCP protocols. If a client specifies to use compression but the service does not support it a protocol exception is thrown indicating a protocol mismatch. For more information, see Choosing a Message Encoder
从 WCF 4.5 开始,WCF 二进制编码器增加了对压缩的支持。压缩类型由 CompressionFormat 属性配置。客户端和服务都必须配置 CompressionFormat 属性。压缩适用于 HTTP、HTTPS 和 TCP 协议。如果客户端指定使用压缩但服务不支持它,则会引发协议异常,指示协议不匹配。有关更多信息,请参阅选择消息编码器
采纳答案by jpw
As per request I copied my comment as answer:
根据要求,我复制了我的评论作为答案:
"Since this property is only exposed on the binaryMessageEncodingBindingElement, you will need to create a custom binding like the following to use this feature:
“由于此属性仅在 binaryMessageEncodingBindingElement 上公开,因此您需要创建如下所示的自定义绑定才能使用此功能:
<customBinding>
<binding name="BinaryCompressionBinding">
<binaryMessageEncoding compressionFormat="GZip"/>
<httpTransport />
</binding>
</customBinding>
and receive compressed messages and therefore the compressionFormat property must be configured on the binaryMessageEncoding element on both client and service. "Both the client and the service need to agree to send
并接收压缩消息,因此必须在客户端和服务的 binaryMessageEncoding 元素上配置 compressionFormat 属性。“客户端和服务都需要同意发送