WCF REST压缩 [英] WCF REST Compression

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

问题描述

我有一个REST服务,返回一大块XML,价值约15万。

I have a REST service that returns a large chunk of XML, about 150k worth.

例如。 http://xmlservice.com/services/RestService.svc/GetLargeXML

因此,我想压缩来自服务器的响应,因为GZIP应该将这个减少到更小。搜索到处无处可见,我不能为我的生活找到一个如何执行压缩WCF REST服务的例子。帮助!!

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!!

注意:我的服务由第三方托管,我不能通过IIS这样做,因为他们不支持。

NOTE: My service is hosted by a third party and I CANNOT do this via IIS as it is not supported by them.

推荐答案

这是很容易做到这一点,至少与.NET 4.0(我没有用3.5测试)。我做的只是让IIS 7照顾它。没有必要创建自定义压缩过滤器。

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.

首先,确保您为IIS 7安装了动态压缩功能。然后,在IIS管理器中选择服务器,使用压缩模块打开动态压缩。或者,您可以从命令行执行此操作:

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:

C:\windows\system32\inetsrv\appcmd set config -section:urlCompression /doDynamicCompression:true 

接下来,编辑以下文件。你可能需要制作一份副本,而不是直接编辑配置(Notepad ++对我抱怨),然后在准备就绪时覆盖原始配置。

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.

C:\Windows\System32\Inetsrv\Config\applicationHost.config

在这里,您会发现一个< dynamicTypes>在< httpCompression>下的部分。在< dynamicTypes>当客户端发送Accept-Encoding:gzip标头时,您需要添加要压缩的所有MIME类型。例如:

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:

<dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/xml" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="*/*" enabled="false" />
</dynamicTypes>

一旦你做了所有的工作,回收你的应用程序池,你应该很好。如果这不起作用,请尝试重新启动服务器,并确保在应用程序级别和服务器级别启用动态压缩。

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.

注意:根据我读过的一些帖子,以前有一个错误,你必须指定字符编码(例如,application / json; charset = utf-8)。但是,我没有任何问题。

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

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