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;
   9:  
  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");
  15:  
  16:             if (!supportGZip)
  17:             {
  18:                 await base.OnActionExecutedAsync(actContext, token);
  19:                 return;
  20:             }
  21:  
  22:             var contentStream = await actContext.Response.Content.ReadAsStreamAsync();
  23:  
  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:             });
  32:  
  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: }

20 comments:

  1. Replies
    1. here is another example, http://blog.developers.ba/asp-net-web-api-gzip-compression-actionfilter/ regards

      Delete
  2. This is open source: https://github.com/Thorium/Owin.Compression

    ReplyDelete
  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

      Delete
  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
    DedicatedHosting4u.com

    ReplyDelete
  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...

    Thanks
    Cpa offers

    ReplyDelete
  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


    ReplyDelete


  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

    ReplyDelete
  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.
    Mattress
    entertainment unit
    furniture nz

    ReplyDelete
  9. wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    This article resolved my all queries.good luck an best wishes to the team members.continue posting.learn digital marketing use these following link
    Digital Marketing Course in Chennai

    ReplyDelete
  10. เราคือผู้นำด้านเกมพนันออนไลน์ Major168 เราคือผู้ให้บริการ คาสิโนออนไลน์ ที่ได้รับรองว่าดีที่สุดในประเทศไทย มีค่ายเกมส์ให้เล่นมากมาย Sagaming, Sexy bacarat, Dreamgame, Ebet, Wm casino, Vivo gaming ไม่ผ่านเอเย่นต์ ระบบปลอดภัยมีทีมงานดูแลตลอด 24ชม.

    SAGAME88 แหล่งรวมเกมส์พนันออนไลน์ คาสิโนสด บาคาร่า กำถั่ว คาสิโนออนไลน์ ไฮโล รูเล็ต รับเครดิตฟรีเล่นได้ทุกเกมส์ โบนัสสมาชิกใหม่เพียบ พร้อมระบบฝากถอนออโต้ 10วิ เรามีทุกค่ายเกมส์ให้คุณเลือกเดิมพัน SA Game Sexy bacarat Dreamgame WM Casino VIVO Gaming Ebet เล่นได้ทุกเกมส์

    Our website ufabet provides betting services in the system of Auto Deposit-Withdrawal. Our members are not only in Thailand. Online football betting UEFA Bet market And this makes a guarantee that Ufabet168 is another reliable football betting website

    ufa through the website UFABET1688 a web gambling online , one that integrated all the bets from online casinos , online casino , online , and also have a game a lot more to be chosen to play such games.

    ReplyDelete
  11. You've made some valid statements there. I looked on the web for extra data about the issue and discovered a great many people will oblige your perspectives on this site.news updates

    ReplyDelete