0

I have an Issue which looks like the one described here

Although the solution doesn't seem to work for me

My target framework is netcoreapp2.2, and I use F#.

I'm working with a Microsoft ASP.NET Core Web API project (normally should work the same as in C#).

I'm trying to return GZip compressed responses I went for the AddResponseCompression middleware solution (keep in mind I'm using fsharp)

Here is my configure method

member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment) =
        
        if (env.IsDevelopment()) 
        then app.UseDeveloperExceptionPage() |> ignore
        else app.UseHsts() |> ignore
            
        app.UseResponseCompression() |> ignore
        app.UseMiddleware<JwtMiddleware>() |> ignore
        app.UseCors "AllowAll" |> ignore
        app.UseMvc() |> ignore
        app.UseSwagger()  |> ignore
        app.UseSwaggerUI(
            fun c ->
                c.SwaggerEndpoint(
                    "/swagger/doc/swagger.json", "Offer Management API")
                |> ignore
        )
        |> ignore

and here my ConfigureServices method

    member this.ConfigureServices(services: IServiceCollection) =
        // Add framework services.
        let configBuilder =
            ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", true)
                .AddJsonFile("appsettings."+this.Env.EnvironmentName+ ".json", true)
                .AddEnvironmentVariables ()
        let config = configBuilder.Build ()
        
        services.Configure<ApiConfig> config |> ignore 
        
        services.AddResponseCompression (
            fun opt ->
                opt.EnableForHttps <- true
                opt.Providers.Add<GzipCompressionProvider>() |> ignore
        ) 
        |> ignore
        
        services.Configure<GzipCompressionProviderOptions>(
            fun (options:GzipCompressionProviderOptions) -> 
                options.Level <- CompressionLevel.Optimal
        ) 
        |> ignore
        
        services.AddCors (
            fun o -> 
                o.AddPolicy (
                    "AllowAll", 
                    fun b -> 
                        b
                            .SetIsOriginAllowed(fun _ -> true)
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials()
                            |> ignore
                )
        ) 
        |> ignore

        services.AddMvc(
            fun config ->            
                config.Filters.Add(OffManExceptionFilter());
        ).AddJsonOptions(
            fun opt ->
                opt.SerializerSettings.Converters.Add (OptionConverter())
                opt.SerializerSettings.Converters.Add(Converters.StringEnumConverter())
                opt.SerializerSettings.ContractResolver <- RequireAllPropertiesContractResolver()
                opt.SerializerSettings.DateTimeZoneHandling <-  DateTimeZoneHandling.Utc
        ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2) 
        |> ignore

As you can see app.UseResponseCompression() is called before app.UseMvc() in the Configure method, and services.AddResponseCompression is called in the ConfigureServices method

The issue is the following...

when I curl my Api I have no difference when I use {"Accept-Encoding" = "gzip"} or not

PS C:\Users\lnimong> curl 'http://localhost:5000/status/dbg' -H @{"Accept-Encoding" = "gzip"}


StatusCode        : 200
StatusDescription : OK
Content           : {"deal_items":[{"id":"002b0c20-d01f-4d3d-804a-851a27427679","catalog_id":62056,"deal_id":"04328e2b-dc0e-4d71-8b5c-db49c02c79d9","article_id":"79810fc1-ea20-4825-9e2b-e6d7cf8bdc8e","raw_article_id":342...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Encoding: gzip
                    Vary: Accept-Encoding
                    Content-Type: application/json; charset=utf-8
                    Date: Tue, 10 Aug 2021 15:39:39 GMT
                    Server: Kestrel

                    {"dea...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Encoding, gzip], [Vary, Accept-Encoding], [Content-Type, application/json; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1334342



PS C:\Users\lnimong> curl 'http://localhost:5000/status/dbg'


StatusCode        : 200
StatusDescription : OK
Content           : {"deal_items":[{"id":"002b0c20-d01f-4d3d-804a-851a27427679","catalog_id":62056,"deal_id":"04328e2b-dc0e-4d71-8b5c-db49c02c79d9","article_id":"79810fc1-ea20-4825-9e2b-e6d7cf8bdc8e","raw_article_id":342...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: application/json; charset=utf-8
                    Date: Tue, 10 Aug 2021 15:39:46 GMT
                    Server: Kestrel

                    {"deal_items":[{"id":"002b0c20-d01f-4d3d-804a-851a27...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, application/json; charset=utf-8], [Date, Tue, 10 Aug 2021 15:39:46 GMT], [Server, Kestrel]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1334342

the RawContentLength is basically the same is both case.

What am I missing ? :(

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
netlyonel
  • 155
  • 1
  • 2
  • 8

2 Answers2

1

After more digging It tuned out it was alrady working from the star I didn't catch the Content-Encoding: gzip in the response

Sorry for the inconvenience

netlyonel
  • 155
  • 1
  • 2
  • 8
0

Interesting fact...

I managed to make it work by creating a custom provider

    
type CustomCompressionProvider() = 
    interface ICompressionProvider with
        member this.CreateStream (outputStream:Stream) = 
            (new GZipStream(outputStream, CompressionLevel.Optimal, true) :> Stream)
        member this.EncodingName:string  = "dbg"
        member this.SupportsFlush = true
...
 services.AddResponseCompression (
            fun opt ->
                opt.EnableForHttps <- true
                opt.Providers.Add<GzipCompressionProvider>() |> ignore
                opt.Providers.Add<CustomCompressionProvider>() |> ignore
        ) 
        |> ignore

but this shouldn't be the solution I still don't understand why the original version fails to work

netlyonel
  • 155
  • 1
  • 2
  • 8