title: WCF GZip Compression Request/Response Processing
body: <p>How do I get a WCF client to process server responses which have been GZipped or Deflated by IIS?</p>
  
  <p>On IIS, I've followed the instructions <a href="http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx" rel="nofollow noreferrer">here</a> on how to make IIS 6 gzip all responses (where the request contained "Accept-Encoding: gzip, deflate") emitted by .svc wcf services.</p>
  
  <p>On the client, I've followed the instructions <a href="https://stackoverflow.com/questions/1365295/wcf-content-length-http-header-on-outbound-message">here</a> and <a href="http://kennyw.com/indigo/153" rel="nofollow noreferrer">here</a> on how to inject this header into the web request: "Accept-Encoding: gzip, deflate".</p>
  
  <p>Fiddler2 shows the response is binary and not plain old Xml.</p>
  
  <p>The client crashes with an exception which basically says there's no Xml header, which ofcourse is true.</p>
  
  <p>In my IClientMessageInspector, the app crashes before AfterReceiveReply is called.</p>
  
  <p>Some further notes:</p>
  
  <p>(1) I can't change the WCF service or client as they are supplied by a 3rd party. I can however attach behaviors and/or message inspectors via configuration if this is the right direction to take.</p>
  
  <p>(2) I don't want to compress/uncompress just the soap body, but the entire message.</p>
  
  <p>Any ideas/solutions?</p>
  
  <p><strong>* SOLVED *</strong></p>
  
  <p>It was not possible to write a WCF extension to achieve these goals. Instead I followed this CodeProject <a href="http://www.codeproject.com/KB/aspnet/CompressibleHTTP20.aspx?msg=1350040" rel="nofollow noreferrer">article</a> which advocate a helper class:</p>
  
  <pre><code>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;
      }
  } 
  </code></pre>
  
  <p>and also, an addition to the application configuration file:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.net&gt;
      &lt;webRequestModules&gt;
        &lt;remove prefix="http:"/&gt;
        &lt;add prefix="http:" 
              type="Pajocomo.Net.CompressibleHttpRequestCreator, Pajocomo" /&gt;
      &lt;/webRequestModules&gt;
    &lt;/system.net&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>What seems to be happening is that WCF eventually asks some factory or other deep down in system.net to provide an HttpWebRequest instance, and we provide the helper that will be asked to create the required instance.</p>
  
  <p>In the WCF client configuration file, a simple basicHttpBinding is all that is required, without the need for any custom extensions.</p>
  
  <p>When the application runs, the client Http request contains the header "Accept-Encoding: gzip, deflate", the server returns a gzipped web response, and the client transparently decompresses the http response before handing it over to WCF.</p>
  
  <p>When I tried to apply this technique to Web Services I found that it did NOT work. Although the helper class was executed in the same was as when used by the WCF client, the http request did not contain the "Accept-Encoding: ..." header.</p>
  
  <p>To make this work for Web Services, I had to edit the Web Proxy class, and add this method:</p>
  
  <pre><code>protected override System.Net.WebRequest GetWebRequest(Uri uri)
  {
      System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
      rq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
      return rq;
  }
  </code></pre>
  
  <p>Note that it did not matter whether the CompressibleHttpRequestCreator and  block from the application config file were present or not. For web services, only overriding GetWebRequest in the Web Service Proxy worked.</p>