Take advantage of ASP.Net Core’s support for response compression middleware to get more compression in less time using Brotli Credit: Thinkstock When working with RESTful services that leverage the ASP.Net Core Web API, the easy availability of CPU versus the scarcity of network bandwidth can be a good reason to use content compression. Content compression reduces bandwidth consumption and facilitates faster responses. Thus you can take advantage of response compression middleware in ASP.Net Core to improve your application’s performance. There are many response compression frameworks available for use with .Net Core. Brotli is a relatively new compression algorithm that provides much improved compression results over Gzip or Deflate, and it is supported by most modern day web browsers including Google Chrome, Mozilla Firefox, Opera, and Microsoft Edge. This article presents a discussion on how we can work with Brotli in ASP.Net Core. Getting started with Brotli in ASP.Net Core The first thing you need to do is create a new ASP.Net Core project in Visual Studio. You should also ensure that the latest version of .Net Core is installed in your system. At the time of this writing, the latest version is .Net Core 2.1. Next, follow these steps to create an ASP.Net Core application in Visual Studio. In the Visual Studio IDE, click on File > New > Project. Select “ASP.Net Core Web Application (.Net Core)” from the list of the templates displayed. Specify a name for the project. Click OK to save. Next, in the “New .Net Core Web Application…” window, select “API.” Specify “ASP.NET Core 2.1” as the version of the ASP.Net Core runtime to be used. Uncheck the “Enable Docker Support,” “Configure for HTTPS,” and “No Authentication” checkboxes. We won’t be needing these features. Click OK. The result should be a new ASP.Net Core project with an example Controller to build and execute RESTful HTTP services. Implementing a response compression provider in ASP.Net Core The client uses the Accept-Encoding header to restrict encoding types in the response. The response compression middleware in ASP.Net Core inspects the request headers to see if there is a compression provider available to handle one of the accepted encodings. The ASP.Net Core response compression middleware uses Gzip compression by default, but you can override this behavior to use custom middleware. In this section we’ll explore how we can build a custom compression provider using Brotli. To get started using Brotli in your ASP.Net Core project, install the Brotli.Net NuGet package via the NuGet package manager. Now, create a class named CustomCompressionProvider that extends the ICompressionProvider interface and implements the CreateStream method as shown below. public class CustomCompressionProvider : ICompressionProvider { public string EncodingName => “br”; public bool SupportsFlush => true; public Stream CreateStream(Stream outputStream) { return new BrotliStream(outputStream, CompressionLevel.Fastest, false); } } Note the usage of CompressionLevel.Fastest to ensure optimal speed. The other possible values are CompressionLevel.NoCompression and CompressionLevel.Optimal. While the former will disable compression, the latter will provide the maximum compression. Naturally, there is always a trade-off between compression level and the time taken to compress content. Configuring response compression in ASP.Net Core To configure Brotli to use with your ASP.Net Core application, you need to take advantage of the Startup class. This class has two methods, Configure and ConfigureServices. The following code snippet illustrates how you can turn on response compression. public void Configure(IApplicationBuilder app) { app.UseResponseCompression(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); } Next, you should specify the response compression middleware to be used. Here’s how. services.AddResponseCompression(options => { options.Providers.Add(new CustomCompressionProvider()); }); The following is the complete code listing of the Startup class for your reference. public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddResponseCompression(options => { options.Providers.Add(new CustomCompressionProvider()); }); } public void Configure(IApplicationBuilder app) { app.UseResponseCompression(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); } } And that’s it. Now when you run your ASP.Net Core Web API services you will see the responses compressed. The response compression middleware in ASP.Not Core provides an elegant way to compress responses using custom compression algorithms. Brotli not only can provide about 20 percent better compression than Gzip, but it is also about 20 times faster at decompressing content. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos