.Net Core/DNX and WebApi compression

971 views Asked by At

I have be seeing a number of examples on how to enable compression in a self-hosted web setup (non IIS).

Eg. here is a created for ASP .NET 5 http://www.erwinvandervalk.net/2015/02/enabling-gzip-compression-in-webapi-and.html

This doesnt work with .NET Core RC1 as the HttpContext.Response.Body stream is marked as non-readable.

How to enable compression in ASP 6/.NET Core RC1 or RC2?

3

There are 3 answers

0
Fabian On

Check out this Post. They use a middleware approach which I just tested and it seems to be working. For asp.net core it would look like this:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(LogLevel.Debug);

    app.Use(async (context, next) => {
        context.Response.Headers.Remove("Content-Type");
        context.Response.Headers.Add("Content-encoding", "gzip");
        context.Response.Headers.Add("Content-Type", "application/json");
        context.Response.Body = new System.IO.Compression.GZipStream(context.Response.Body, System.IO.Compression.CompressionMode.Compress);
        await next();
        await context.Response.Body.FlushAsync();
    })
    .UseMvc();
}

You can also try to play around with IResultFilter.OnActionExecuted as it is possible that the result is just not computed yet.

0
KenL On

I have been testing using http://www.webpagetest.org/ with my AspNetCore site hosted on SmarterAsp.Net -> I had to do the following to get an A for compression.

Add this dependency:

"System.IO.Compression": "4.1.0-rc2-24027", 

And my startup.cs:

public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            context.Response.Headers.Add("Content-encoding", "gzip");
            context.Response.Body = new System.IO.Compression.GZipStream(context.Response.Body,
                System.IO.Compression.CompressionMode.Compress);
            await next();
            await context.Response.Body.FlushAsync();
        });
0
Reint Jan Hoiting On

Compression is a new feature at ASP.net Core 1.1:

This is the packages you will need. https://www.nuget.org/packages/Microsoft.AspNetCore.ResponseCompression/

here is some official microsoft video showing how to use it: https://youtu.be/IfLg6LQCl-Y?t=223

some background information: https://github.com/aspnet/BasicMiddleware/issues/34

Code:

Project.json:

"dependencies": {
   ...,
   "Microsoft.AspNetCore.ResponseCompression": "1.0.0"
}

Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  ...
  //Add Middleware
  app.UseResponseCompression();
  ...
}

public void ConfigureServices(IServiceCollection services)
{
  //Configure Compression level
  services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);

  //Add Response compression services
  services.AddResponseCompression(options =>
  {
    options.Providers.Add<GzipCompressionProvider>();
  });
}