Friday, February 5, 2010

Applying GZIP Compression to WCF Services

Hi guys!

Tweaking the web application's performance
Improving the web application's performance is getting more and more important these days. Recently, I have researched the approaches to gzip(compress) your WCF Service responses, which reduces the responses size in common 5 to 1 ratio. This results in faster transfer of the responses and less bandwidth. For instance, if you had responses with size 30kb, they will become 6kb when compressed, without losing anything. Sounds great, doesn't it?

3 easy ways to enable GZIP compression on your WCF Service:
1. Using IIS7
In case you are using IIS7, enabling the GZIP compression for the server's responses comes on the fly: http://blogs.iis.net/ksingla/archive/2006/06/13/changes-to-compression-in-iis7.aspx
As you see, using IIS7 to compress the WCF responses is easy and straight-forward, but not everyone is running IIS7 on their production servers. So, let's think of other options, not including the usage of IIS7.

2. Writing a custom message encoder
Fortunately, Microsoft have covered this case, too. You can use their WCF sample for Custom Message Encoder: http://msdn.microsoft.com/en-us/library/ms751458.aspx and use it in your WCF Services. You have to read and understand the sample, then modify it for your needs.

3. Using Telerik RadCompression
In case, you are finding the Custom Message Encoder sample too complicated or you are trying to use your WCF Service with Silverlight(which currently supports only basic/binary bindings), the easiest way to compress the responses, without having hard time with making things work is using the Telerik's RadCompression: http://www.telerik.com/help/aspnet-ajax/radcompression.html. Using the RadCompression, you just define the RadCompression HttpModule in your WCF Service's web.config and it instantly starts working, even when communicating with Silverlight.

In conclusion
As I had to apply gzip compression for a WCF Service, communicating with a Silverlight Application (using a Custom Binary Encoding - http://blogs.msdn.com/silverlightws/archive/2009/03/20/what-s-new-with-web-services-in-silverlight-3-beta.aspx), without using an IIS7, I had hard time trying to modify the Custom Message Encoder Sample to work in my case, so I went using the Telerik's RadCompression module, which is very fast and easy to setup.

Happy optimizing!