Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSAzureEnableCompression-9f4d9b73

Today’s code sample demonstrates how to add new mime types for static compression in Windows Azure.  It is written by Microsoft Escalation Engineer - Narahari Dogiparthi.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.



Static compression is the feature that is shipped out of the box in IIS. Using static compression, developers/administrators can enable faster downloads of their web site static content like javascripts, text files, Microsoft office documents, html/htm files, cs files, etc.  So, how can we make use of this feature when hosting the web application in Windows Azure? By default static compression is enabled in Windows Azure, however, there are only few mime types that will be compressed. This sample demonstrates adding new mime types for static compression.


Building the Sample

This sample can be run as-is without making any changes to it.


Running the Sample

  1. Open the sample on the machine where VS 2010, Windows Azure SDK 1.6 are installed
  2. Right click on the cloud service project i.e. CSAzureEnableCompression and choose Publish
  3. Follow the steps in publish Wizard and choose subscription details, deployment slots, etc. and enable remote desktop for all roles
  4. After successful publish, browse to a page that has javascript files and then login to Azure VM and verify that the cache directory has the compressed files.


Using the Code

To customize static compression settings in Windows Azure, you can use startup tasks. Below are the steps I have followed to successfully enable static compression for few of the mime types my application needed.

1. Configure following tag in Application’s web.config @Configuration/System.WebServer

<urlCompression doStaticCompression="true"/>

2. Create iisconfigchanges.cmd file with required commands to customize ApplicationHost.config configuration of IIS

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/x-javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/vnd.openxmlformats-officedocument.presentationml.presentation',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:serverruntime /frequentHitThreshold:1 /commit:APPHOST
exit /b 0

Above commands configures MIME types for javascripts, word documents (docx), Excel documents(xlsx), Powerpoint documents(pptx). If you need to compress any specific files other than mentioned above, find out the MIME types per your requirement and add similar commands to the file.
Note: I have changed frequentHitThreshold parameter since I could not see compression happening without explicitly specifying this parameter.
3. Add below startup task that will execute iisconfigchanges.cmd during startup of the rule. This configuration should be added in ServiceConfiguration.csdef under webrole/workerrole tag.

  <Task commandLine="iisconfigchanges.cmd" executionContext="elevated" taskType="simple" /> 

4. Add iisconfigchanges.cmd file to webrole/workerrole project

5. Configure below file properties for iisconfigchanges.cmd file , so that it will be copied to bin directory

     Build Action : Content
      Copy To Output Directory : Copy Always

More Information

Caution: Compression settings should be tweaked carefully, It might result in undesired performance too if not configured properly. For example, images like png are already compressed and compressing these types again, will cause additional CPU on the system without any significant gain in the bandwidth. I recommend you to research and thoroughly test your application with the compression settings before you apply the changes to production.  I recommend below blog entry for further reading on IIS7 compression.
IIS 7 Compression. Good? Bad? How much?  http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx