Click here to Skip to main content
Click here to Skip to main content

WCF does not support Compression out of the box, so fix it

By , 8 Aug 2011
 
WCF service and client do not support HTTP Compression out of the box in .NET 3.5 even if you turn on Dynamic Compression in IIS 6 or 7. It has been fixed in .NET 4 but those who are stuck with .NET 3.5 for the foreseeable future, you are out of luck. First of all, it’s IIS's fault that it does not enable http compression for SOAP messages even if you turn on Dynamic Compression in IIS 7. Secondly, it’s WCF’s fault that it does not send the Accept-Encoding: gzip, deflate header in http requests to the server, which tells IIS that the client supports compression. Thirdly, it’s again WCF's fault that even if you make IIS send back compressed response, WCF can’t process it since it does not know how to decompress it. So, you have to tweak IIS and System.Net factories to make compression work for WCF services. Compression is key for performance since it can dramatically reduce the data transfer from server to client and thus give significant performance improvement if you are exchanging medium to large data over WAN or internet.
 
There are two steps – first configure IIS, then configure System.Net. There’s no need to tweak anything in WCF like using some Message Interceptor to inject HTTP Headers as you find people trying to do here, here and here.
 
After you have enabled Dynamic Compression on IIS 7 following the guide, you need to add the following block in the <dynamicTypes> section of applicationHost.config file inside C:\Windows\System32\inetsrv\config folder. Be very careful about the space in mimeType. They need to be exactly the same as you find in response header of SOAP response generated by WCF services.
 
<add mimeType="application/soap+xml" enabled="true" />
<add mimeType="application/soap+xml; charset=utf-8" enabled="true" />
<add mimeType="application/soap+xml; charset=ISO-8895-1" enabled="true" />
 
After adding the block, the config file will look like this:
 
For IIS 6, first you need to first enable dynamic compression and then allow the .svc extension so that IIS compresses responses from WCF services.
Follow this guide for IIS 6 to enable dynamic compression in IIS 6 for specific URLs.[^]
 
Next, you need to make WCF send the Accept-Encoding: gzip, deflate header as part of request and then support decompressing a compressed response.
 
You need to override the System.Net default WebRequest creator to create HttpWebRequest with compression turned on. First, you create a class like this:
 
public class CompressibleHttpRequestCreator : IWebRequestCreate
{
    public CompressibleHttpRequestCreator()
    {
    }
 
    WebRequest IWebRequestCreate.Create(Uri uri)
    {
        HttpWebRequest httpWebRequest =
            Activator.CreateInstance(typeof(HttpWebRequest),
            BindingFlags.CreateInstance | BindingFlags.Public |
            BindingFlags.NonPublic | BindingFlags.Instance,
            null, new object[] { uri, null }, null) as HttpWebRequest;
 
        if (httpWebRequest == null)
        {
            return null;
        }
 
        httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip |
            DecompressionMethods.Deflate;
 
        return httpWebRequest;
    }
}
 
Then on the WCF Client application’s app.config or web.config, you need to put this block inside system.net which tells system.net to use your factory instead of the default one.
 
<system.net>
  <webRequestModules>
    <remove prefix="http:"/>
    <add prefix="http:" type="WcfHttpCompressionEnabler.CompressibleHttpRequestCreator, WcfHttpCompressionEnabler,
        Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </webRequestModules>
</system.net>
 
That’s it.
 
I have uploaded a sample project (http://code.msdn.microsoft.com/WCF-Compression-Example-9ffab49c[^] which shows how all this works.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omar Al Zabir
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: thnx again :)memberSelvin25 Mar '11 - 3:22 
thnx again Smile | :)
GeneralMaybe a NOOB questien but when you write "allow the .svc ext...memberPeter Geyfman8 Aug '11 - 6:20 
Maybe a NOOB questien but when you write "allow the .svc extension so that IIS compresses responses from WCF services" (for the IIS 6 instructions), how do I do that?

I followed this example to a letter, but I am not getting the compressed output out of my WCF service.

Thanks.
GeneralRe: I have updated the tip with the link to IIS 6 guide. It was ...memberOmar Al Zabir8 Aug '11 - 6:34 
I have updated the tip with the link to IIS 6 guide. It was missing.
GeneralReason for my vote of 5 wow i couldn't find this 5x thnx man...memberSelvin25 Mar '11 - 1:17 
Reason for my vote of 5
wow i couldn't find this
5x thnx man
i was trying to find the way to compress Microsoft Sync Framework service response/request without luck ... now it's worrking Smile | :)
GeneralYou don't need to use reflection to instantiate an HttpWebRe...memberjim lahey16 Mar '11 - 3:49 
You don't need to use reflection to instantiate an HttpWebRequest:
 
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=VS.90%29.aspx
 
Just create a plain WebRequest and cast it in.net 3.5.
GeneralRe: WebRequest.Create uses the IWebRequest factory pattern. So, ...memberOmar Al Zabir25 Mar '11 - 1:19 
WebRequest.Create uses the IWebRequest factory pattern. So, it gets into infinite loop.
GeneralCode Project has PRE tags you can use to make your code look...protectorAspDotNetDev15 Mar '11 - 11:40 
Code Project has PRE tags you can use to make your code look better (adds font colors, keeps indents, changes background color, uses fixed width font, color is specific to language you specify). Your tip/trick really needs them.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 8 Aug 2011
Article Copyright 2011 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid