在 Silverlight 中向 WCF 添加压缩的最简单方法是什么?
我有一个访问 WCF Web 服务的 silverlight 2 beta 2 应用程序。 因此,目前只能使用 basicHttp 绑定。 Web 服务将返回相当大量的 XML 数据。 从带宽使用的角度来看,这似乎相当浪费,因为如果压缩,响应会小 5 倍(我实际上将响应粘贴到 txt 文件中并对其进行压缩。)。
该请求确实有“Accept-Encoding:gzip,deflate” - 有没有办法让WCF服务gzip(或以其他方式压缩)响应?
我确实找到了这个 链接 但它恕我直言,对于应该开箱即用处理的功能来说,确实似乎有点复杂。
好的 - 起初我使用 System.IO.Compression 标记解决方案作为答案,因为我永远无法“似乎”让 IIS7 动态压缩发挥作用。 事实证明:
IIS7 上的动态压缩一直一直有效。 只是 Nikhil 的 IE Web Developer Helper 插件没有显示它工作。 我的猜测是,由于 SL 将 Web 服务调用交给浏览器,因此浏览器会“在幕后”处理它,而 Nikhil 的工具永远不会看到压缩的响应。 我可以通过使用 Fiddler 来确认这一点,Fiddler 监视浏览器应用程序外部的流量。 在 fiddler 中,响应实际上是 gzip 压缩!!
System.IO.Compression 解决方案的另一个问题是 System.IO.Compression 在 Silverlight CLR 中不存在。
因此,从我的角度来看,在 Silverlight 中启用 WCF 压缩的最简单方法是在 IIS7 中启用动态压缩并且根本不编写任何代码。
I have a silverlight 2 beta 2 application that accesses a WCF web service. Because of this, it currently can only use basicHttp binding. The webservice will return fairly large amounts of XML data. This seems fairly wasteful from a bandwidth usage standpoint as the response, if zipped, would be smaller by a factor of 5 (I actually pasted the response into a txt file and zipped it.).
The request does have the "Accept-Encoding: gzip, deflate" - Is there any way have the WCF service gzip (or otherwise compress) the response?
I did find this link but it sure seems a bit complex for functionality that should be handled out-of-the-box IMHO.
OK - at first I marked the solution using the System.IO.Compression as the answer as I could never "seem" to get the IIS7 dynamic compression to work. Well, as it turns out:
Dynamic Compression on IIS7 was working al along. It is just that Nikhil's Web Developer Helper plugin for IE did not show it working. My guess is that since SL hands the web service call off to the browser, that the browser handles it "under the covers" and Nikhil's tool never sees the compressed response. I was able to confirm this by using Fiddler which monitors traffic external to the browser application. In fiddler, the response was, in fact, gzip compressed!!
The other problem with the System.IO.Compression solution is that System.IO.Compression does not exist in the Silverlight CLR.
So from my perspective, the EASIEST way to enable WCF compression in Silverlight is to enable Dynamic Compression in IIS7 and write no code at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 IIS7,请查看 压缩模块。 这允许您为服务器的 HTTP 请求配置压缩。
If you are using IIS7, take a look at the Compression Module. This allows you to configure compression for HTTP requests to your server.
最近做WCF项目的时候没有看到WCF原生的方式来做压缩。 我只是使用 System.IO.Compression 命名空间并制作了一个快速压缩器。 这是我使用的代码
,然后我只是让我的服务接受字节数组作为输入,就像这样
对我来说效果很好。
I didn't see a native way for WCF to do compression when doing a WCF project recently. I just used the System.IO.Compression namespace and made a quick compressor. Here's the code i used
then i just had my services take in a byte array as an input, like such
Worked out well for me.
还应该注意的是,除了为站点启用压缩之外,您可能还需要将 mime 类型添加到
部分下的applicationHost.config
:如果某些动态响应没有被压缩(有些是),则可能是 MIME 类型问题。 使用 Fiddler 获取与请求相关的详细信息。 失败的请求跟踪可能有助于确定 IIS 是否正在尝试压缩响应。 如果压缩配置正确,您将在跟踪输出的完整跟踪部分看到
NO_MATCHING_CONTENT_TYPE
。It should also be noted that you may need to add the mime type to
applicationHost.config
under<httpCompression><dynamicTypes>
section in addition to enabling compression for the site:If certain dynamic responses are not being compressed (and some are) it could be a mime type issue. Use Fiddler to get the specifics associated with the request. Failed request tracing may be useful in determining whether or not IIS is even attempting to compression the response. If compression is correctly configured you will see a
NO_MATCHING_CONTENT_TYPE
in the complete trace section of the trace output.WCF 的 WS-压缩允许您在绑定上配置压缩。
请参阅 Pablo M. Cibraro 的 WS-Compression for WCF
或者,尝试 Microsoft < a href="http://msdn.microsoft.com/en-us/library/cc138373.aspx" rel="nofollow noreferrer">GZip 编码器示例 “创建使用 System.IO 的编码器通道.Compression.GZipStream 类用于压缩传出的 WCF 消息”
WS-Compression for WCF allows you to configure compression on the binding.
See WS-Compression for WCF by Pablo M. Cibraro
Alternatively, try Microsofts GZip Encoder Sample which "creates an encoder channel that uses the System.IO.Compression.GZipStream class to compress outgoing WCF messages"