2

I am unable to get Gzip compression middleware to work in asp.net core 2.0 (or 2.1). I created a new project using the "Blank" Web Application template and have the following code in Startup.cs:

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

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();

    app.Run(async (context) =>
        {
            context.Response.Headers.Add("Content-Type", "text/plain");
            await context.Response.WriteAsync("Hello World!");
        });
}

I verified that Chrome is sending "Accept-Encoding: gzip, deflate, br" in the request.

Accept-Encoding

However, the server is not gzip-encoding the response:

Content-Encoding

What am I doing wrong?

I found this discussion, and tried everything from there, but it didn't help.

Mark Miretsky
  • 43
  • 1
  • 6
  • Try [Troubleshooting](https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression#troubleshooting) and let us know everything that you've ruled out. – Mark G Jun 13 '18 at 20:41
  • I went through the Troubleshooting section and verified that everything in my example is correct, as stated there. I verified using both Chrome and Fiddler. This issue can be easily reproduced. First create an empty project in Visual Studio 2017 (just use the "Empty" ASP.NET Core template). I tried it with .NET Core 2.0 and 2.1 with the same result. Then open up Startup.cs and edit it according to the [documentation](https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.1&tabs=aspnetcore2x#configuration). You will notice that gzip is not working. – Mark Miretsky Jun 15 '18 at 14:19
  • The middleware expects your response to include a `Content-Type` header. If I take your repro steps and add `context.Response.Headers.Add("Content-Type", "text/plain");` before the call to `WriteAsync`, the compression kicks in. Here's the [code](https://github.com/aspnet/BasicMiddleware/blob/release/2.1/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionProvider.cs#L116) that shows that logic in the middleware services. – Kirk Larkin Jun 15 '18 at 16:02
  • Still doesn't work. I modified the code as you suggested (see updated code above), but compression is still not kicking in (although I can see the "Content-Type" header in the response now. – Mark Miretsky Jun 15 '18 at 18:05
  • Must be something else then. You’re not using https, right? – Kirk Larkin Jun 15 '18 at 18:33
  • No, I am using HTTP. – Mark Miretsky Jun 15 '18 at 18:34
  • You might need to provide a github repo or similar - the steps from your question work for me. – Kirk Larkin Jun 15 '18 at 18:56
  • Are you using IIS Express or Kestrel? Try the other and see if you get the same results. – Mark G Jun 15 '18 at 18:57
  • @KirkLarkin - if you created your project using the "Empty" ASP.NET Core 2.0 (or 2.1) template in Visual Studio and modified `Startup.cs` as above, then you and I are running the same code. Sounds like it's something with my environment. I am running Windows 10 if that makes any difference. Probably not because I have the same issue on my Windows 7 laptop. – Mark Miretsky Jun 15 '18 at 19:45
  • @MarkG - I am hosting the application in Kestrel with IIS Express as the reverse proxy, which is the default configuration. Are you suggesting to host the application in IIS Express? – Mark Miretsky Jun 15 '18 at 19:52
  • @MarkMiretsky Try it without IIS Express then and directly with Kestrel on port 5000. – Mark G Jun 15 '18 at 20:14
  • Still no compression when I run it directly with Kestrel (using `dotnet run`) on port 5000. – Mark Miretsky Jun 15 '18 at 21:00
  • Looks like we’re running out of options. How are you determining that the compression is not kicking in? – Kirk Larkin Jun 15 '18 at 21:27
  • I am looking at the Network tab in Chrome Dev Tools. Nothing shows up in the Content-Encoding column, and when I select the "localhost" record, I see `Accept-Encoding: gzip, deflate, br` under Request Headers, but do not see `Content-Encoding: gzip` under Response Headers. I _do_ see `Content-Type: text/plain` under Response Headers, however. – Mark Miretsky Jun 15 '18 at 21:36
  • @MarkMiretsky I just tried the same code on my PC and it works fine. I added a screenshot in my answer below to demonstrate the request / response. Really hard to know why you have this issue on both your Win 7 & Win 10 computers. – Mark G Jun 15 '18 at 22:06
  • It's a mystery... Thanks guys for your time. If I do end up figuring it out, I will post the answer. – Mark Miretsky Jun 15 '18 at 22:43

5 Answers5

7

I faced a similar problem (asp.net core on Mac)

Solution was to make sure response compression was called BEFORE static file serving.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCookiePolicy();

        // ordering matters here
        app.UseResponseCompression();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

After ordering it correctly I could see it in chrome under encoding br (Brotli Compression):

enter image description here

AntonB
  • 2,724
  • 1
  • 31
  • 39
  • I ran into a similar configuration issue. Working on a project someone else started, it looked like the `services.AddResponseCompression` and `app.UseResponseCompression` where setup ... but on closer inspection, they were using a 3rd party library that set the response-content incorrectly for only some `Html.Partial(` calls. Switching from 3rd party to [the Microsoft way](https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.2) fixed it. Very frustrating to track down. – BurnsBA Aug 21 '19 at 20:57
1

TL;DR: The code is fine, the issue with an environment.

First, please find out if you have any antivirus that intercepts incoming HTTP traffic. In my case, it was ESET Endpoint Antivirus. Second, Check your response for the presence of header Vary: Accept-Encoding. That indicates that Kestrel processed your request correctly and paid attention to Accept-Encoding request header.

If you do have antivirus what happens is that it sniffs incoming unencrypted traffic, sees gzip header and unpack message to check for malware or whatever. Then it passes the decompressed response to a browser and removes gzip content-encoding header.

You should turn off antivirus for some time, or test this feature on the environment without one.

Quodnon
  • 43
  • 5
  • That's very interesting. We do, in fact, have ESET Endpoint Antivirus installed company-wide. I will see if I can disable the antivirus and try it again. – Mark Miretsky Jul 20 '18 at 23:22
  • Disabling ESET did not fix the issue for me. – Mark Miretsky Aug 17 '18 at 18:51
  • Thanks, it resolved my issue. How to disable HTTP checking - https://github.com/cdnjs/cdnjs/issues/4815#issuecomment-118029930 – Mayo May 30 '19 at 20:06
0

From ASP.NET Core Middleware:

The following example demonstrates a middleware ordering where requests for static files are handled by the static file middleware before the response compression middleware. Static files are not compressed with this ordering of the middleware.

Below a screen capture of Fiddler showing the request and response with corresponding headers:

enter image description here

Mark G
  • 2,848
  • 1
  • 24
  • 32
  • It doesn't work even if I don't use MVC or static files. I just edited the code in my original question with the simplified example. – Mark Miretsky Jun 15 '18 at 15:49
0

I was facing the same problem.

My observation is, Fiddler somehow doesn't show Content-Encoding, if it is done through ASP.net core middlewear. I am not sure why.

But If I look at the same request in Chrome debugger, I am able to see that information with the compressed data.

  1. First row in image is without compression
  2. Second row in image is with compression

enter image description here

vishal shah
  • 177
  • 5
0

If you use Fiddler, make sure the "Decode" function disabled or Fiddler will auto decode. disable decode

Jim
  • 489
  • 5
  • 11