Web API using Gzip Compression

This article shows how easy it is to extend Web API and provides an example of GZIP compression. Ove Andersen has created a cool NuGet package which makes it very is to use GZIP compression with any Web API Http request/response. This package implements both client and server message handlers which can be used for the whole service or as required.

Demo Code: https://github.com/damienbod/WebApiGzip

To use this package, install it from NuGet:
webapigzip_01

or
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/

Then add the message handler to the Web API config:

config.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

Now all Web API requests, will return a GZIP compressed response if requested in the header.
webapigzip_02

The NuGet package is very easy to use and the source code is also available:
https://github.com/azzlack/Microsoft.AspNet.WebApi.MessageHandlers.Compression

The package can also be configured to only compress messages larger than a certain size, this is documented here.

Ben Foster also provides a nice implementation and also a good article showing how you could implement GZIP compression with Web API.
http://benfoster.io/blog/aspnet-web-api-compression

As you can see, Web API is easy to extend, you can use a finished NuGet package to implement GZIP for your service, or you can implement it yourself following the code examples.
You have the choice. Thanks to the 2 authors for the GZIP implementations.

Links:

https://github.com/azzlack/Microsoft.AspNet.WebApi.MessageHandlers.Compression

https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/

http://benfoster.io/blog/aspnet-web-api-compression

6 comments

  1. Reblogged this on jogendra@.net.

  2. Hi Damien, Thanks a lot for your post. I tried to follow all the steps mentioned in the post and found that compression is working fine with the default setting.

    config.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

    But once I set the threshold value as per the advanced usage details :

    config.MessageHandlers.Insert(0, new ServerCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor()));

    I see in the Fiddler that the response is not getting compressed even if the Content-Length is more than 4096 bytes.

    Can you please help me to know is there any else I am missing out anything else.

    1. Hi

      post your question/bug here

      https://github.com/azzlack/Microsoft.AspNet.WebApi.MessageHandlers.Compression/issues

      I’ll have a look once I get some time, I’m a bit busy at the moment

      Gruss Damien

  3. your project is not working
    responses are sent without compression

    1. Hi Eric, strange, I’ll have a look, haven’t changed anything here in a long time.

      Greetings Damien

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.