Saturday, November 15, 2008

WS Response: XML Compression

Compressing the XMLs sent over the wire helps reduce the network traffic significantly. Compressing responses on the server and decompressing them on the client consumes additional CPU cycles on both server and client, but the savings in bandwidth is likely to justify the processing cost.

XML compression can be implemented by using one of the following techniques:

1. Set EnableDecompression property

EnableDecompression property gets or sets a Boolean that indicates whether decompression is enabled for this HttpWebClientProtocol. Default value for the property is “False”. The property is supported in .Net framework 2.0 and above.

E.g.

WebServiceRef.Service service = new WebServiceRef.Service();

service.EnableDecompression = true;

When the server returns the results, they are in gzip format, deflated format, or uncompressed. The property is only a suggestion, not a demand.

2.     By extending proxy class in client for the compression and decompression of requests and responses

To enable compression in .Net 1.1 GetWebRequest, GetWebResponse methods needs to be overridden in the proxy class generated. As the modified code will be removed if web reference needs to be added again, instead of modifying proxy code a class is inherited from proxy class and the methods are overridden in that class.

This method is also useful when the message created by proxy needs modification before sending it.

E.g.

protected override WebRequest GetWebRequest(Uri uri)

// Update the request's HTTP headers to specify that

// we can accept compressed (gzipped, deflated) responses

          {

                   WebRequest request = base.GetWebRequest(uri);

                   if (compressResponse)

                   {

                             request.Headers.Add("Accept-Encoding", "gzip, deflate");

                   }

                             return request;

                   }

protected override WebResponse GetWebResponse(WebRequest request)

// If we've requested compressed responses, return a WebResponse

// derivative that's capable of uncompressing it.

          {

                   WebResponse result;

                   // If user checked the Compressed Response checkbox

                   if (compressResponse)

                   {

                             result = new CompressedWebResponse((HttpWebRequest) request);

                   }

                   else // no compression requested, return stock WebResponse object

                   {

                             result = base.GetWebResponse(request);

                   }

                   // Keep track of content length to measure bandwidth savings.

                   responseContentLength = result.ContentLength;

                   return result;

          }

The CompressedWebResponse class uses the #ziplib library (available athttp://www.icsharpcode.net/OpenSource/SharpZipLib/) to decompress responses.

3.     Use HTTP compression features available in IIS 5.0 and later versions for compressing the response from the Web services.

Note that you need a decompression mechanism on the client and client should request the server for zipped response (This could be done by point 1 and 2 above).

To enable IIS 5.0 to compress .aspx pages, follow these steps:

Open a command prompt.

1.      Type net stop iisadmin, and then press ENTER.

2.      Type cd C:\InetPub\adminscripts, and then press ENTER.

3.      Type the following, and then press ENTER:

4.      CSCRIPT.EXE ADSUTIL.VBS SET     W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx" Type the following, and then press ENTER:

5.      CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx"

6.      Type net start w3svc, and then press ENTER

No comments: