views:

608

answers:

4

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.

A: 

Here is a link that might be suitable:

WS-Compression for WCF

At the very least, it should put you on the right track.

David
Thanks, but unfortunately that is a WS (SOAP) solution and not REST (webHttp).
PhilJ
A: 

The easiest way would be to use the PLJ Compression filter in front of your servlet.

PJL Compression Filter

This looks for the Accept-Encoding Http Header to determine if the client can accept a compressed response. It basically just wraps the ServletOutputStream in a GZipOutputStream or DeflateerOutputStream - pretty simple and useful.

Gandalf
This is a WCF Service running on IIS, so a J2EE servlet filter is obviously not an option.
PhilJ
My bad, didn't have a clue what WCF was - figured it was another Java-esque framework.
Gandalf
+1  A: 

Carlos Figueira from MSFT has kindly provided a solution:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/8c3eafae-b6a1-441f-85ef-90721d941a1a

PhilJ
+1  A: 

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.

Kurt