Sunday, 3 October 2010

How to enable wcf messages compression in Silverlight (4) and IIS 7

There are many tutorials that explain how to do this. But these are very comprehensive for
the many platforms that can be envolved.

Here i will talk only about my particular situation:

  • Silverlight 4
  • A Wcf Service with the default visual studio 2010 binding configuration:
   1 <configuration>
2 <system.serviceModel>
3 <bindings>
4 <customBinding>
5 <binding name="CustomBinding_ServizioVisualizzatore" >
6 <binaryMessageEncoding />
7 <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
8 </binding>
9 </customBinding>
10 </bindings>
11 <client>
12 <endpoint address="http://localhost:4534/Visualizzatore.Web/ServizioVisualizzatore.svc"
13 binding="customBinding" bindingConfiguration="CustomBinding_ServizioVisualizzatore"
14 contract="Servizio.ServizioVisualizzatore" name="CustomBinding_ServizioVisualizzatore" />
15 </client>
16 </system.serviceModel>
17 </configuration>

  • IIS 7 on Windows Server 2008 32 bit
Let's begin! First of all you won't have to touch your code, everything is done with one or two settings inside IIS.

IIS will send the data you asked to it( making a call to the web service) to the client in gzip format if the client says that it can manage compressed responses.

How can we see if our client Silverlight application is 'happy' to receive a gzipped response?
Well, let's install Fiddler and have a look at the http request of the service call:

(Please click on the image to see full size)
In the Request headers of the wcf call you can read 'Accept encoding: gzip, deflate'.
It means that the client is saying that it can accept a compressed response.
Below you can see (Transformer tab of the http responses pane) that the content of the response is actually gzipped as we desire.

The last thing to note is the Content-Type of our http request. It is set to 'application/soap+msbin1', we'll use it later.

Now, it's time to set some options within IIS 7:

(Please click on the image to see full size)

The screenshot (Italian language) shows the icon that you should press to open the section where you can enable static and dynamic contents compression.

After this you should open the IIS applicationhost.config file. You should find it in the 'C:\Windows\System32\inetsrv\config' directory.
Here you can add the line '' (please note "application/soap+msbin1") in the httpCompression - dynamicTypes section of the file obtaining something similar to this:
   1 <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
2 <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
3 <dynamicTypes>
4 <add mimeType="text/*" enabled="true" />
5 <add mimeType="message/*" enabled="true" />
6 <add mimeType="application/x-javascript" enabled="true" />
7 <add mimeType="application/soap+msbin1" enabled="true" />
8 <add mimeType="*/*" enabled="false" />
9 </dynamicTypes>
10 <staticTypes>
11 <add mimeType="text/*" enabled="true" />
12 <add mimeType="message/*" enabled="true" />
13 <add mimeType="application/javascript" enabled="true" />
14 <add mimeType="*/*" enabled="false" />
15 </staticTypes>
16 </httpCompression>

Now it should be all done :-). Alternatively you could also set <add mimeType="*/*" enabled="true" /> to enable compression for all kinds of content.

1 comment:

Unknown said...

This was exactly what I was looking for. Thanks.