如何在 WCF 4.5 中使用 gzip 压缩 [英] How to get gzip compression working in WCF 4.5

查看:23
本文介绍了如何在 WCF 4.5 中使用 gzip 压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WCF 4.5 支持 GZIP,无需第三方库或手写扩展.我通过 TCP 绑定让它工作,但找不到通过 HTTP 绑定让它工作的方法.我的 wcf - 服务自托管在 Windows 服务中.

插件:我不允许使用 IIS;我无法切换到任何 WCF 替代品.

这适用于 gzip:

binding="customBinding" bindingConfiguration="tcpCompressionBinding" name="tcp"

这是我目前用于 http 的:

binding="basicHttpBinding" bindingConfiguration="httpBinding" name="http"

文档并没有真正帮助我:http://msdn.microsoft.com/en-us/library/dd456789.aspx.

但是,根据这个它应该可以工作:

<块引用>

从 WCF 4.5 开始,WCF 二进制编码器增加了对压缩的支持.压缩类型由 CompressionFormat 属性配置.客户端和服务都必须配置 CompressionFormat 属性.压缩适用于 HTTP、HTTPS 和 TCP 协议.如果客户端指定使用压缩但服务不支持它,则会引发协议异常,指示协议不匹配.有关详细信息,请参阅选择消息编码器

解决方案

根据要求,我复制了我的评论作为答案:

<块引用>

"由于这个属性只暴露在binaryMessageEncodingBindingElement,您将需要创建一个自定义如下绑定以使用此功能:

<绑定名称="BinaryCompressionBinding"><binaryMessageEncoding compressionFormat="GZip"/><http传输/></binding></customBinding>

<块引用>

并接收压缩消息,因此压缩格式必须在 binaryMessageEncoding 元素上配置属性客户和服务."客户端和服务都需要同意发送

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.

Addon: i am not allowed to use IIS; i can't switch to any WCF replacement.

this works with gzip:

binding="customBinding" bindingConfiguration="tcpCompressionBinding" name="tcp" 

and this is what i currently use for http:

binding="basicHttpBinding" bindingConfiguration="httpBinding" name="http"

The documentation does not really help me: http://msdn.microsoft.com/en-us/library/dd456789.aspx.

But, according to this it should work:

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

解决方案

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:

<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

这篇关于如何在 WCF 4.5 中使用 gzip 压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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