本文介绍了使 IIS gzip 压缩与 .NET 4.0 上的 Silverlight WCF 服务一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Silverlight 项目,它通过 WCF Web 服务从 IIS 7.0 服务器获取数据.返回的数据通常很大,因此压缩似乎是一个不错的选择.但是,对于我的一生,我无法让它发挥作用.

I have a Silverlight project getting data from an IIS 7.0 server via WCF web services. The data returned is usually large, so compression seemed like a good option. However, for the life of me, I can't get it to work.

这是我目前尝试过的

  • 为所有 mime 类型 (/) 启用压缩(动态和静态).验证它有效(在 aspx 页面上的点击返回按 fiddler 压缩的数据)
  • 对 WCF 和压缩进行了大量挖掘.我读了一些关于 WCF 4.5 的内容,有一些东西可以启用/禁用 gzip.我不确定这是否意味着无法使用 IIS 7.0 动态压缩,或者是否与此无关.
  • 还得到了一些使用 GZipEncoder 的示例(来自 Microsoft WCF 示例)
  • Enabled compression (dynamic and static) for all mime types (/). Verified that it works (a hit on an aspx page returns gzipped data as per fiddler)
  • Did a whole lot of digging around on WCF and compression. I read something along the lines of WCF 4.5 having something to enable / disable gzip. I'm not sure if this means that IIS 7.0 dynamic compression cannot be used, or if its something unrelated.
  • Also got a few examples on using GZipEncoder (from Microsoft WCF Samples)

这就是我想知道的

  • 使用 Silverlight 从托管在 IIS 7.0 服务器上的 WCF 读取数据使用 .NET 4.0,是否可以为 XML 打开压缩通过简单地弄乱 Web.Config/applicationHost.Config?
  • 如果没有,让它工作的最简单方法是什么(比如,添加一个 dll,更改我的 Web.Config 并完成!)

谢谢各位!

感谢大家的回答.只是一个简短的说明 - 如果有人可以确认它不适用于 WCF 4.0或"仅当您这样做时才适用于 WCF 4.0,那会有所帮助.

Thanks for all the answers folks. Just one quick note - if someone can confirm that it does not work for WCF 4.0 "or" works with WCF 4.0 only if you do this, that'd help.

推荐答案

我们已经为 WCF 使用了 GZip 压缩,我将尝试回溯我们为使其工作所做的工作.有一些问题可能会困扰您以使其正常工作(就我个人而言,我正在尽可能地远离 WCF).

We've got GZip compression working for WCF, I'll try to backtrack what we did to get it working. There are a few gotcha's which might bite you trying to get this to work (personally I'm moving trying to move away from WCF wherever possible).

首先您需要安装 IIS 动态压缩.其次,您必须在 Silverlight 应用程序中使用浏览器 http(这是浏览器中的默认设置,但在运行 OOB 时不是),因为内置的 http 堆栈不支持 GZip 压缩.您可以在 Application_Startup() 函数中强制执行此操作(如果需要,将 http 更改为 https).

Firstly you need to have IIS Dynamic Compression installed. Secondly you must use browser http in the Silverlight app (which is the default in browser, but not when running OOB) since the build-in http stack doesn't support GZip compression.You can force this in the Application_Startup() function (change http to https if required).

WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.BrowserHttp);

这里的问题是,当 Web 服务调用失败时,您只会在客户端中遇到一般错误,因为浏览器堆栈没有将 http 500 响应的内容传递给 Silverlight.这对您来说是否是一个问题,它可以通过自定义 MessageInspector 解决,它将响应代码始终更改为 200.

The gotcha here is that you will only ever get generic errors in your client when a webservice call fails because the browser stack doesn't pass the content of an http 500 response on to Silverlight. Is this is an issue for you it can be worked around with a custom MessageInspector which changes the response code to 200 always.

在您的 web.config 中,您需要启用动态压缩

In your web.config you need to enable dynamic compression

<system.webServer>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

最重要的是,您需要在 IIS 中注册 mime 类型,我们将其添加到 applicationHost.config 中,用于 WCF 的二进制消息编码.

On top of that you need to have the mime type registered in IIS, we added this to the applicationHost.config for WCF with binary message encoding.

<add mimeType="application/soap+msbin1" enabled="true" />

如果您不使用二进制消息编码,您可能需要 application/soap+xml 来代替这里.如果我没记错的话,这些就是所有需要的步骤.

If your not using binary message encoding you probably need application/soap+xml instead here. If I recall correctly these are all the steps needed.

最后一个问题,当你的客户端发送大消息时它没有帮助,因为没有办法(afaik)让silverlight实际向服务器发送gzip压缩请求,所以你唯一的收获将是数据发送由服务器.

The last gotcha though, it doesn't help when your client is sending large messages since there is no way (afaik) to make silverlight actually send gzip compressed requests to the server, so your only gain will be on the data send by the server.

这篇关于使 IIS gzip 压缩与 .NET 4.0 上的 Silverlight WCF 服务一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:45