【问题标题】:Issue with GZIP in ASP.NET Core MVC with F#带有 F# 的 ASP.NET Core MVC 中的 GZIP 问题
【发布时间】:2021-08-10 15:43:34
【问题描述】:

我有一个看起来像here 描述的问题

虽然该解决方案似乎对我不起作用

我的目标框架是netcoreapp2.2,我使用F#。

我正在使用 Microsoft ASP.NET Core Web API 项目(通常应该与 C# 中的工作方式相同)。

我正在尝试返回 GZip 压缩响应 我选择了AddResponseCompression middleware solution(请记住我使用的是 fsharp)

这是我的配置方法

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

这里是我的 ConfigureServices 方法

    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

如您所见,在 Configure 方法中 app.UseMvc() 之前调用了 app.UseResponseCompression(),而在 ConfigureServices 方法中调用了 services.AddResponseCompression

问题如下...

当我卷曲我的 Api 时,我是否使用 {"Accept-Encoding" = "gzip"} 没有区别

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

RawContentLength 基本上都是一样的情况。

我错过了什么? :(

【问题讨论】:

  • 有趣的事实(或解决方法:))我设法使它与自定义压缩提供程序一起工作

标签: c# .net-core f# asp.net-core-webapi gzip


【解决方案1】:

经过更多挖掘 它调出它已经从星星开始工作了 我没有在回复中看到Content-Encoding: gzip

很抱歉给您带来不便

【讨论】:

    【解决方案2】:

    有趣的事实...

    我设法通过创建自定义提供程序使其工作

        
    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
    

    但这不应该是解决方案 我还是不明白为什么原来的版本不能用了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多