Adding Gzip Compression to .Net Core

In this article I would like to Explore the usage of GZip compression in the Server-side for a .Net Core web application.

Scenario

I am sending a JSON object list consisting of 1 Thousand items.  In the ordinary response format it is taking 1 MB of file size and 1 Second to receive in the client-side.

I am using an Azure S2 Service for deployment & testing.

The Challenge

Following is the Chrome display of the URL Statistics.

image

The Solution

Now we are trying to achieve the solution using ASP.NET Core Response Compression.  For this we need to add the Library mentioned below to the application.

image

The Code

In the Service.cs file add the following highlighted lines of code.

public void ConfigureServices(IServiceCollection services)
         {
             services.AddMvc();

            services.AddResponseCompression(options =>
             {
                 options.Providers.Add<GzipCompressionProvider>();
                 options.MimeTypes =
                     ResponseCompressionDefaults.MimeTypes.Concat(
                         new[] { “text/json”, “application/json” });
             });


            services.Configure<GzipCompressionProviderOptions>(options =>
             {
                 options.Level = CompressionLevel.Optimal;
             });

         }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
         {
            app.UseResponseCompression();

            if (env.IsDevelopment())
             {
                 app.UseDeveloperExceptionPage();
             }

            app.UseMvc();
         }

Now compile, deploy & retest with the Chrome browser.

You can see there is 90% reduction in size of the response!

The response time also got reduce by 70%.

image

The Client Code

HttpClientHandler handler = new HttpClientHandler()
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

               };
                var client = new HttpClient(handler);
                client.BaseAddress = new Uri(URL);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));

               Stopwatch watch = Stopwatch.StartNew();

               HttpResponseMessage response = client.GetAsync(“api/kpi/list”).Result;
                response.EnsureSuccessStatusCode();

               double ms = watch.ElapsedMilliseconds;

               Console.WriteLine(“Elapsed Milliseconds: ” + ms.ToString());

Summary

The above code shows the components & code required to add JSON compression to your .Net Core application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s