Dynamic compression is all about optimally using your transport channel. The data flowing through the channel can be pretty large and thereby may stress the bandwidth usage. Simply put its like the how you send a zipped document as attachment in your mail instead of the actual document.
Practical Scenarios
An application has to upload/download a large file (in MB's or GB's) through web service. Such sizes would eat up the bandwidth and you would see the application performance deteriorating. You would see the web site (service hosts) struggling.
Using IIS 6.0 as Web Server (Windows 2003 Server)
IIS Configurations
On IIS 6.0, you had to install the Web Service Extension 3.0 to enable the HTTP compression using the GZip.
- Open up IIS and right-click on the web sites node and go to Properties.
- Click on Service tab
There are two options:
- Isolation mode
- HTTP compression
- Within HTTP compression check both the check boxes
- Compress application files
- Compress static files
- Click on OK and close the dialog box
- Go to Web Service Extensions node. Right-click on the right pane, and click Add a new Web service extension
- The New Web Service Extension dialog box will appear
- Enter the Extension name. Recommended: to use 'HTTP Compression’
- Click on 'Add’. Browse to the Gzip dll. Ideally it would be in the path: C:\WINDOWS\system32\inetsrv\gzip.dll and click on 'OK’ button.
- Check the 'Set extension status to Allowed’ and click on 'OK’ button.
- Check the 'Set extension status to Allowed’ and click on 'OK’ button.
- Edit the Metabase.xml. In IIS, right-click on the top node and check 'Enable Direct Metabase Edit’.
- Open windows explorer and browse to inetsrv folder. (C:\WINDOWS\system32\inetsrv)
- Find Metabase.xml and take a backup of the file.
- Open the Metabase.xml in a text editor. Find the <IISCompressionScheme/> section. Do be careful, there are two sections here: One for deflate and other for GZip.
- You need to use GZip section. Update the Location attribute to the following value:
Location = "/LM/W3SVC/Filters/Compression/gzip" - Look for the HcScriptFileExtensions section. The default schema should have: asp, dll and exe.
This is where you need to add file extensions for ones which have to be compressed. - If you need to support the compression for webservice, then add the 'asmx’ file details
- Restart the IIS
How to handle the proxy through which you consume the web service on the client side?
After you add your web reference, the proxy is created. It’s a partial class, so you can add in some custom code in another file and extend the same class.
Here in trap the web request and add the http header.
public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest request = base.GetWebRequest(uri);
request.Headers.Add("Accept-Encoding", "gzip, deflate");
return request;
}
Using IIS 7.0 as Web Server (Windows 2008 Server)
IIS Configurations
<httpCompression directory="E:\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" dynamicCompressionLevel="4" />
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
</httpCompression>
- Configure compression feature for IIS server level
- Select the server name in the IIS
- In the features, select (double click on) 'Compression’
- Check if the 'Static content compression’ is enabled.
- Check the cache directory details
In the IIS select the virtual directory
- In the features, select (double click on) 'Compression’
- The compression details will open up
- Check the dynamic content compression check box
- Click on 'Apply’ link on the Alerts box on the right hand side of the screen
How to handle the proxy through which you consume the web service on the client side?
Here is the funny thing, the partial class extension you would have written within your client to handle the request to IIS6 doesn’t work here.
Don’t know why.
You need to modify the call in there.
public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
protected override WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
return request;
}