Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable IIS compression for WCF services?

I currently use a custom gzip encoder for my WCF service. I want to replace it with the built-in IIS 7 compression if that is possible. I can't find info online on how to that.

Is there any way to enable IIS 7 compression for WCF services? Do you know if this will be supported out-of-the-box with .Net 4?

Edit June 15th: I'm still looking for a way to replace our custom gzip encoder with a mainstream approach so if you know how to do that with .Net 4 and IIS 7, please answer this question.

Thanks


Sidebar : My attempt at doing this manually

Since I can't find how to do it by simply turning a few knobs I decided to try and enable it manually.

So far I have:

  • Installed and enabled the IIS 7 Dynamic Compression Module
  • Changed the section of the applicationHost.config file to enable compression for mimeType="application/soap+xml" and mimeType="application/xop+xml".

I used an HTTP sniffer to sniff traffic sent from my app (Windows Forms). I see that requests do not have the Accept-Encoding:gzip,deflate http header.

So I

  • Added it manually to all outgoing calls using the OperationContextScope class and its OutgoingMessageProperties. (I will post the details later if I find the solution).

With the http sniffer, I can see that the client header now has the correct header:

POST /### path to my service ####/MyService.svc HTTP/1.1
MIME-Version: 1.0
Content-Type: multipart/related; type="application/xop+xml";
    start="<http://tempuri.org/0>";
    boundary="uuid:####### some uuid #############";
    start-info="application/soap+xml"
Accept-Encoding: gzip,deflate
Host: ####### my server name #############
Content-Length: 1753
Expect: 100-continue

But the server response is still not compressed.

Why is the server response not compressed? Have I used the correct mime types? Once I get the server to return a compressed answer, will the client automatically decompress it or will have to write code on the client side to decompress?


Thanks for your help

like image 228
Sylvain Avatar asked Nov 14 '09 18:11

Sylvain


2 Answers

I had the same problem; .aspx pages were compressed but WCF content wasn't. It has to do with the content type returned by the WCF service, which got appended to the mime-type.

I got it to work with the following section in the ApplicationHost.config:

<dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/*" enabled="true" />
    <add mimeType="*/*" enabled="false" />
</dynamicTypes>

Here's what I did (most of the same steps as mentioned already):

  • Install the Dynamic Compression Role Service for IIS role
  • Enable Dynamic Content Compression for the website that you use to host the WCF service
  • Open %SystemRoot%\system32\inetsrv\config\applicationhost.config and add the relevant content type to the section of the
  • After this it still didn't work.
  • I checked the data with Firefox' Tamper Data and noticed the content type returned was actually "application/xml; charset=utf-8".
  • After adding the complete content type, including the "; charset=utf-8" to the section, it worked:
<add mimeType="application/xml; charset=utf-8" enabled="true" />

As I felt that the character set encoding should not be determining if the compression works or not, I ended up letting IIS compress all application/* content types.

like image 76
Arjan van der Meer Avatar answered Oct 21 '22 22:10

Arjan van der Meer


This is useful for IIS 6

http://ramon.bloggingabout.net/2008/11/06/wcf-and-http-gzipdeflate-compression-and-silverlight/

(updated URL)

like image 40
Simon_Weaver Avatar answered Oct 21 '22 23:10

Simon_Weaver