Sunday, February 15, 2015

Enabling GZip Compression in WebAPI and Owin

Interesting little challenge today. I’m building an application that uses Web API and will run in an OWIN SelfHost and my payload as big enough to warrant GZip Compression. But, since my app is running as an Owin Selfhosted api, I can’t use IIS Compression.

So I created a little compression attribute. Seems to work great.

   1: namespace eVision.AppShaping.Handlers.Controllers
   2: {
   3:     using System.IO.Compression;
   4:     using System.Linq;
   5:     using System.Net.Http;
   6:     using System.Threading;
   7:     using System.Threading.Tasks;
   8:     using System.Web.Http.Filters;
  10:     internal class GzipCompressedAttribute : System.Web.Http.Filters.ActionFilterAttribute
  11:     {
  12:         public override async Task OnActionExecutedAsync(HttpActionExecutedContext actContext, CancellationToken token)
  13:         {
  14:             bool supportGZip = actContext.Request.Headers.AcceptEncoding.Any(x => x.Value == "gzip");
  16:             if (!supportGZip)
  17:             {
  18:                 await base.OnActionExecutedAsync(actContext, token);
  19:                 return;
  20:             }
  22:             var contentStream = await actContext.Response.Content.ReadAsStreamAsync();
  24:             actContext.Response.Content = new PushStreamContent(async (stream, content, context) =>
  25:             {
  26:                 using(contentStream)
  27:                 using (var zipStream = new GZipStream(stream, CompressionLevel.Optimal))
  28:                 {
  29:                     await contentStream.CopyToAsync(zipStream);
  30:                 }
  31:             });
  33:             actContext.Response.Content.Headers.Remove("Content-Type");
  34:             actContext.Response.Content.Headers.Add("Content-encoding", "gzip");
  35:             actContext.Response.Content.Headers.Add("Content-Type", "application/json");
  36:         }
  37:     }
  38: }


  1. Replies
    1. here is another example, regards

  2. This is open source:

  3. Replies
    1. Great Article
      Cloud Computing Projects

      Networking Projects

      Final Year Projects for CSE

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  4. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks

  5. I am overwhelmed by your post with such a nice topic. Usually, I visit your blogs and get updated with the information you include but today’s blog would be the most appreciable...

    Cpa offers

  6. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application


  7. Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
    what is seo
    types of seo

  8. TreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
    entertainment unit
    furniture nz