WCF REST 压缩
我有一个 REST 服务,它返回一大块 XML,大约价值 150k。
例如 http://xmlservice.com/services/RestService.svc/GetLargeXML
因此我想要压缩来自服务器的响应,因为 GZIP 应该将其减少到更小的值。在到处搜索后,我无法找到如何对 WCF REST 服务执行压缩的示例。帮助!!
注意:我的服务由第三方托管,我无法通过 IIS 执行此操作,因为他们不支持它。
I have a REST service that returns a large chunk of XML, about 150k worth.
e.g. http://xmlservice.com/services/RestService.svc/GetLargeXML
Therefore I want to compress the response from the server, as GZIP should reduce this to something much smaller. Having searched everywhere I cannot for the life of me find an example of how to perform compression for WCF REST services. Help!!
NOTE: My service is hosted by a third party and I CANNOT do this via IIS as it is not supported by them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,做到这一点非常容易,至少在 .NET 4.0 中是这样(我没有用 3.5 进行测试)。我所做的就是让 IIS 7 来处理它。无需创建自定义压缩过滤器。
首先,确保您已安装 IIS 7 的动态压缩功能。然后,在 IIS 管理器中选择服务器并使用压缩模块打开动态压缩。或者,您可以从命令行执行此操作:
接下来,编辑以下文件。您可能必须复制它,而不是直接编辑配置(Notepad++ 对我抱怨),然后在准备好时覆盖原始文件。
在那里你会发现一个下的部分。在下当客户端发送 Accept-Encoding: gzip 标头时,您需要添加要压缩的所有 mime 类型。例如:
完成所有这些后,回收您的应用程序池,您就可以开始了。如果这不起作用,请尝试重新启动服务器并确保在应用程序级别和服务器级别打开动态压缩。
注意:根据我读过的一些帖子,曾经存在一个错误,您必须指定字符编码(例如,“application/json; charset=utf-8”)。不过,我没有遇到任何问题。
It is actually pretty easy to do this, at least with .NET 4.0 (I didn't test with 3.5). What I do is just let IIS 7 take care of it. There is no need to create a custom compression filter.
First, make sure you have installed the Dynamic Compression feature for IIS 7. Then, select the server in IIS Manager and use the compression module to turn on Dynamic Compression. Alternatively, you can do this from the command line:
Next, edit the following file. You may have to make a copy of it rather than editing the config directly (Notepad++ complains for me), then overwrite the original when you are ready.
In there you will find a <dynamicTypes> section under <httpCompression>. Under <dynamicTypes> you will need to add all the mime types you want to be compressed when the client sends an Accept-Encoding: gzip header. For example:
Once you've done all that, recycle your application pool and you should be good to go. If that doesn't work, try restarting your server and ensuring that dynamic compression is turned on at the application level as well as the server level.
Note: According to some posts I've read, there used to be a bug where you had to specify the character encoding (e.g., "application/json; charset=utf-8"). However, I didn't have any problems.