Response Compression in ASP.NET Core

As everyone well knows by now, ASP.NET Core, unlike previous ASP.NET versions, was not designed to be run just on IIS. ASP.NET Core comes with a built-in lightweight web server. Usually, you would run this behind an IIS or NGINX Reverse Proxy but it is also possible to use it on it's own.

Now as part of ASP.NET Core 1.1, Microsoft introduced Response Compression Middleware in ASP.NET Core.

To start using the Microsoft.AspNetCore.ResponseCompression Middleware, first we have to get the NuGet Package in our .csproj:

   <PropertyGroup>
      <TargetFramework>netcoreapp1.1</TargetFramework>
      <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
      <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
      <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
 +    <PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="1.0.0" />
      <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
      <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
    </ItemGroup>

Once we have gotten the NuGet Package, we can now configure the Compression Module in the void ConfigureServices(IServiceCollection) section in our Startup.cs:

          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
 +            //Configure Response Compression
 +            services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
 +            services.AddResponseCompression(options =>
 +            {
 +                options.Providers.Add<GzipCompressionProvider>();
 +            });
 +
              // Add framework services.
              services.AddMvc();
          }

Since we have configured the GZip Compression Module, we can now go ahead and load the Response Compression Middleware in the void Configure(IApplicationBuilder, IHostingEnvironment, ILoggerFactory) in our Startup.cs:

         // 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)
         {
+              app.UseResponseCompression();
               app.UseStaticFiles();
               app.UseMvc();
         }

As you can see, using the Chrome Developer Tools we can confirm that the Response is being GZip Compressed as expected:
Confirming Compression

For a working example, on GitHub cdemi/ASP.NET-Core-Response-Compression, I have deployed the default ASP.NET Core Web App Template and added Response Compression.

If you want to see the changes needed to add Response Compression you can look at commit f377ebdd4c2184744eb9249cfc0de74f8805505c, which will give you a diff from the default Microsoft Template.