It can be helpful to withstand the compression of responses when they are large, which can greatly reduce the time taken to download them.

Activate gzip compression

To enable gzip compression is very simple the ASP.NET Core framework provides ready-made classes.

In the Startup file, simply enable the response compression as follows:


using Microsoft.AspNetCore.ResponseCompression;

...

public ConfigureServices(IServiceCollection services)
{
     services.AddResponseCompression();
}

Note that you can also configure the Gzip compression level as follows:


services.Configure<GzipCompressionProviderOptions>(o => o.Level = System.IO.Compression.CompressionLevel.Optimal);

If your site is in https you can enable compression as follows:


services.AddResponseCompression(o =>
{
     o.EnableForHttps = true;
});

Then in the Configure method you must call the following extension method:


app.UseResponseCompression();

Activate the Brotli and Deflate compression

ASP.NET Core does not provide compression provider for both formats.

So we have to write them down anyway.

Compression provider Brotli:


public class BrotliCompressionProvider : ICompressionProvider
{
    private readonly CompressionLevel _compressionLevel;
    public BrotliCompressionProvider(CompressionLevel compressionLevel = CompressionLevel.Fastest)
    {
        _compressionLevel = compressionLevel;
    }

    public string EncodingName => "br";
    public bool SupportsFlush => true;
    public Stream CreateStream(Stream outputStream)
    {
       return new BrotliStream(outputStream, _compressionLevel);
    }
}

Activate the deflate compression

Compression provider Default:


public class DeflateCompressionProvider : ICompressionProvider
{
    private readonly CompressionLevel _compressionLevel;
    public DeflateCompressionProvider(CompressionLevel compressionLevel = CompressionLevel.Fastest)
    {
        _compressionLevel = compressionLevel;
    }

    public string EncodingName => "deflate";
    public bool SupportsFlush => true;
    public Stream CreateStream(Stream outputStream)
    {
       return new DeflateStream(outputStream, _compressionLevel);
    }
}

Note that the DeflateStream class does not meet the Deflate standard well so if you use a client other than .NET it will not be able to decode your file.

Then the AddResponseCompression method must be modified as follows:


services.AddResponseCompression(o =>;
{
   o.Providers.Add(new BrotliCompressionProvider());
   o.Providers.Add(new DeflateCompressionProvider());
   o.EnableForHttps = true;
});

Recovering a Compressed Answer

Now that we have a compressed answer there is no need to use it.

To have compressed content the client must send the Accept-Encoding header with gzip, or deflate, or br value.

It can also send the 3 gzip,deflate,br” to tell the server that it supports the 3 formats.

But customers do not automatically decompress the answers:

Thankfully my library Tiny. RestClient automatically does.

tt The HttpClient API class enables automatic decompression of compressed content.

But its use is not obvious because it has to be handled specifically:


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


using (var client = new HttpClient(handler))
{
    ....
}

Watch out for it.’At the moment the compression broke n’is not yet managed by HttpClient API.

When do you need to activate the compression?

The documentation specifies that files less than 1000 bytes should not be compressed as this can produce a compressed file larger than the uncompressed file.

It is also worth noting that compression adds to the server-side computing load.

Happy coding.

To go further