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 ? :(