WCF REST Compression

这一生的挚爱 提交于 2019-11-28 08:44:22

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:

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

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!