Web API Compression Using GZip

Web API compression is very important to improve ASP.Net Web API performance. Data travels through the network in packages (data packets). Reducing data packet size improves the load performance. In this article, I explain how to compress a Web API method and see how it improves the performance of Web API.

There are many ways by which we can compress Web API. Here I will implement it using an actionFilter that can be used on the method level, controller level and entire WebAPI.

For this, I will use a popular library for compression called DotNetZip library and this library can be easily downloaded from NuGet.



First create a simple solution and add a Web API project. Then add a class called CompressFilter.cs to the Filters folder.



Add a method called DeflateCompression.
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. using System.IO;  
  5.   
  6. using System.IO.Compression;  
  7.   
  8. using System.Linq;  
  9.   
  10. using System.Net.Http;  
  11.   
  12. using System.Web;  
  13.   
  14. using System.Web.Http.Filters;  
  15.   
  16. namespace Test.API.Filters  
  17.   
  18. {  
  19.   
  20.     public class DeflateCompressionAttribute: ActionFilterAttribute  
  21.   
  22.     {  
  23.   
  24.         public override void OnActionExecuted(HttpActionExecutedContext actContext)  
  25.   
  26.         {  
  27.   
  28.             var content = actContext.Response.Content;  
  29.   
  30.             string contentencoding = ApiHeaderValue.AppContentEncoding.ToString();  
  31.   
  32.             if (contentencoding == "GZip")  
  33.   
  34.             {  
  35.   
  36.                 var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result;  
  37.   
  38.                 var zlibbedContent = bytes == null ? new byte[0] :  
  39.   
  40.                 CompressionHelper.DeflateByte(bytes);  
  41.   
  42.                 actContext.Response.Content = new ByteArrayContent(zlibbedContent);  
  43.   
  44.                 actContext.Response.Content.Headers.Remove("Content-Type");  
  45.   
  46.                 actContext.Response.Content.Headers.Add("Content-encoding""GZip");  
  47.   
  48.                 actContext.Response.Content.Headers.Add("Content-Type""application/json");  
  49.   
  50.                 base.OnActionExecuted(actContext);  
  51.   
  52.             }  
  53.   
  54.         }  
  55.   
  56.     }  
  57.   
  58.     public class CompressionHelper  
  59.   
  60.     {  
  61.   
  62.         public static byte[] DeflateByte(byte[] str)  
  63.   
  64.         {  
  65.   
  66.             if (str == null)  
  67.   
  68.             {  
  69.   
  70.                 return null;  
  71.   
  72.             }  
  73.   
  74.             using(var output = new MemoryStream())  
  75.   
  76.             {  
  77.   
  78.                 using(  
  79.   
  80.                 var compressor = new Ionic.Zlib.GZipStream(  
  81.   
  82.                 output, Ionic.Zlib.CompressionMode.Compress,  
  83.   
  84.                 Ionic.Zlib.CompressionLevel.BestSpeed))  
  85.   
  86.                 {  
  87.   
  88.                     compressor.Write(str, 0, str.Length);  
  89.   
  90.                 }  
  91.   
  92.                 return output.ToArray();  
  93.   
  94.             }  
  95.   
  96.         }  
  97.   
  98.     }  
  99.   

In the preceding class, a value is passed using headers in a variable called contentencoding. It contains the value as GZip/Deflate/No. If No is passed, then the compression method will not be applicable and plain JSON data will be returned.

Then use the following class at the method level as in the following:

.

 

Execute the Web API method by commenting the DeflateCompression attribute that is applied on the Web API controller method, you will find that returned data is not compressed but simple JSON.

 

See the execution time when compression is not applied.



Now uncomment the DeflateCompression attribute and pass either GZip or Deflate, then the method will return compressed data and it will be 85% (approx.) less in size.




See the execution time when compression is applied.


 

We find that when compression is not applied then the content-length is 22583 otherwise 2783. So the difference is about 87% of data is compressed. Thus small data will travel in the network and this improves the performance of the Web API. I have used Fiddler and Postman tools for testing this application.