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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos