title: Transparent GZIP decompression of POST Data for WCF Rest Service
body: <p>This is our scenario (which is not negotiable):</p>
  
  <ul>
  <li>WCF REST Service exposed over HTTP using WebHttpEndpoint hosted in IIS7</li>
  <li>All responses and POST request data transmitted as JSON</li>
  <li>Non-WCF Web Client</li>
  <li>We've activated gzip compression for JSON responses in IIS7 which works fine. </li>
  </ul>
  
  <p>Since we are doing post requests with larger JSON payloads we've implemented client side GZIP compression for JSON post data and setting the "Content-Encoding" header to "gzip". Unfortunately the IIS does not handle this out of the box. The post data reaches the WCF deserializer in compressed form which of course results in an exception. </p>
  
  <p>I've tried various extension points to hook into the WCF pipeline but the only promising solution (Operation Behavior) didn't work because in absence of a WCF client, the ApplyClientBehavior method of the IOperationBehavior interface will never be called.</p>
  
  <p>In the end if implemented a HttpModule which gets the job done but I'm not exactly happy with the result because of the following caveats:</p>
  
  <ul>
  <li>Though I am able to transparently decompress the request data by setting the Filter property of the current HttpRequest to a GZipInputStream that's only half of the solution because WCF insists on reading exactly HttpRequest.ContentLength bytes from the request which for compressed requests will be obviously much less than the uncompressed payload</li>
  <li>For some weird reason beyond my imagination Microsoft has blocked every legal way to change the ContentLength of the request. In the end I had to modify the private backing field for the ContentLength property of the request. Which is not something you want to do in production code.</li>
  <li>Microsoft also made it impossible to read the request InputStream in HttpModule to figure out the uncompressed content length which required our web client to also pass a custom header containing the uncompressed content length</li>
  </ul>
  
  <p>All in all this feels like an awful lot of work that couldn't even be implemented cleanly so I would like to know if someone can point out alternatives to implementing the decompression part in IIS. I'm absolutely fine with recommendations for a commercial product that does this if there is one.</p>