Response Compression Middleware in ASP.NET Core
Response Compression is if client browser supports response compression, the server sends the content compressed, so the response size is reduced. There are couple of compression schemes, but almost all the browsers support gzip and deflate. In this post, let’s see how you can use Response Compression Middleware in an ASP.NET Core application to serve the content compressed using gzip.
Here let's use Visual Studio 2017 RC and create an ASP.NET Core Web Application targeting .NET Core and let's select the template as Web API as we want to simulate large content being sent to the client.
Once the project is created and all the dependencies are restored, let’s update all our dependencies to ASP.NET Core 1.1. Please note that Visual Studio 2017 RC targets ASP.NET Core 1.0.1 for its default ASP.NET Core templates as it’s the LTS (Long Term Support) version as of now.
https://lh3.googleusercontent.com/-YUHOV5WMsm8/WDUqhgj7-NI/AAAAAAAAEQk/vkYg5YE1xx0/image_thumb%25255B8%25255D.png?imgmax=800 |
Update Nuget Packages |
Once the update is complete, let’s modify the Get() action in default ValuesController to return some large set of data.
[HttpGet]
public IEnumerable<string> Get()
{
List<string> someStrings = new List<string>();
for (int i = 0; i < 100000; i++)
{
someStrings.Add($"Value{i}");
}
return someStrings;
}
Now let’s just run the application, trigger Get() action in ValuesController and explore the request and response information.
First, if we examine the request headers, it looks as follows.
https://lh3.googleusercontent.com/-WcccCi-Xlvc/WDUqjL4ZLpI/AAAAAAAAEQs/wlQiv6Ogm_M/image_thumb%25255B11%25255D.png?imgmax=800 |
Request and Reponse Headers |
In this we are using Google Chrome and it seems that our current version of Chrome supports a set of compression types. But in the response there was no indication the content which got received is compressed.
https://lh3.googleusercontent.com/-bMfzsK7mdRA/WDUqkzgGvZI/AAAAAAAAEQ0/f_iJg_u6iho/image_thumb%25255B17%25255D.png?imgmax=800 |
Response Size |
Size of the content received is 1.2 MB.
Now let’s add some code to send the content compressed using gzip. Let’s add a new NuGet package to the project and that is Microsoft.AspNetCore.ResponseCompression.
https://lh3.googleusercontent.com/-PbRQTIB06yw/WDUqmLYiqSI/AAAAAAAAEQ8/s3cOkkEn46c/image_thumb%25255B4%25255D.png?imgmax=800 |
Install Microsoft.AspNetCore.ResponseCompression |
Now let’s modify the ConfigureServices method and Configure method in startup.cs to add and use Response Compression Middleware.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
});
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseResponseCompression();
app.UseMvc();
}
You will need to add the following using.
using Microsoft.AspNetCore.ResponseCompression;
Now let’s run the application again, trigger Get() action in ValuesController and examine the request and response.
https://lh3.googleusercontent.com/-2CFZHniMopA/WDUqnyMljXI/AAAAAAAAERE/nKMwxGCbSkA/image_thumb%25255B20%25255D.png?imgmax=800 |
Request and Reponse Headers |
The request is obviously the same and now you can see that response is being compressed using gzip.
https://lh3.googleusercontent.com/-d3PNRAHyDm0/WDUqoyKQW_I/AAAAAAAAERM/tL55cUYnN0k/image_thumb%25255B27%25255D.png?imgmax=800 |
Response Size |
And this time only 238 KB was transferred. 1.2 MB has been reduced to 238 KB.
Happy Coding.