WCF REST Compression

喜夏-厌秋 提交于 2019-11-27 02:25:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1402295/wcf-rest-compression

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