【问题标题】:Response compression in asp.net core 2.2 not workingasp.net core 2.2中的响应压缩不起作用
【发布时间】:2019-11-01 12:48:36
【问题描述】:

我正在使用 asp.net core 2.2、Microsoft.EntityFrameworkCore(2.2.4)、Microsoft.EntityFrameworkCore.Cosmos(2.2.4)、GraphQL.NET 来开发基于 graphql 的 API。

基于链接:https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0

我在 Startup.cs 类方法中配置了设置以启用压缩。

这是我的代码:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    string serviceEndPoint = Configuration.GetValue<string>("CosmosDBEndpoint");
    string authKeyOrResourceToken = Configuration.GetValue<string>("CosmosDBAccessKey");
    string databaseName = Configuration.GetValue<string>("CosmosDBName");
    services.AddEntityFrameworkCosmos();
    services.AddDbContext<TaxathandDbContext>(options => options.UseCosmos(serviceEndPoint, authKeyOrResourceToken, databaseName, contextOptions =>
    {
        contextOptions.ExecutionStrategy(d => new CosmosExecutionStrategy(d));
    }

    ));
    // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    }

    );
    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<IDocumentWriter, DocumentWriter>();
    services.AddScoped<IUtilityService, UtilityService>();
    services.AddScoped<ICommonService, CommonService>();
    services.AddScoped<ICountryService, CountryService>();
    services.AddScoped<CountryResultType>();
    services.AddScoped<GraphQLQuery>();
    services.AddScoped<ICountriesResolver, CountriesResolver>();
    services.AddScoped<CountryType>();
    services.AddScoped<Response>();
    services.AddScoped(typeof(ResponseGraphType<>));
    services.AddScoped(typeof(ResponseListGraphType<>));
    services.AddTransient<IAddressRepository, AddressRepository>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
    services.AddGraphQL(o =>
    {
        o.ExposeExceptions = true;
    }

    ).AddGraphTypes(ServiceLifetime.Scoped).AddDataLoader();
    services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
    services.AddScoped<SampleTestSchema>();
    services.AddResponseCompression(o =>
    {
        o.EnableForHttps = true;
    }

    );
    services.Configure<BrotliCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    }

    );
    services.Configure<GzipCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    }

    );
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseFetchLocaleMiddleware();     
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseGraphQL<SampleTestSchema>();
    app.UseGraphQLPlayground(options: new GraphQLPlaygroundOptions());      
    app.UseResponseCompression();
    app.UseMvc();
}

代码构建良好,在使用 Postman 对其进行验证时,我通过了以下标头:

接受编码:br 内容类型:应用程序/json 接受语言:en-us

根据上述 MSDN 文档,服务器应发送标头 Content-Encoding:br 和 Vary:Accept-Encoding。

谁能帮我解决这个问题?

【问题讨论】:

  • 认为您仍然需要在您的services.AddResponseCompression 设置中添加提供者:services.AddResponseCompression(options =&gt; { options.Providers.Add&lt;BrotliCompressionProvider&gt;(); options.Providers.Add&lt;GzipCompressionProvider&gt;(); });(但我不确定,或者我会将此作为答案发布)
  • 根据 MSDN 链接:docs.microsoft.com/en-us/aspnet/core/performance/… 它们默认可用。我做了另一个 POC,发现工作正常,但上面发布的代码对我不起作用。
  • 您使用的是什么服务器?您提供的链接表明 Kestrel 和 Http.sys 均不提供内置压缩支持。
  • 因此我使用 asp.net core 提供的内置中间件来执行压缩。
  • 一个小更新,我正在使用 Azure Dev Spaces 运行项目,在响应中我看到 Server: Kestrel, Content-Type:application/json, Transfer-Encoding:chunked

标签: c# asp.net-core asp.net-core-2.2 ef-core-2.2 graphql.net


【解决方案1】:

终于解决了。我通过将行:app.UseResponseCompression() 移动到方法的顶部更新了 Startup.cs 代码中的 Configure 方法。我对其进行了验证,发现工作正常。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();
    app.UseFetchLocaleMiddleware();     
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseGraphQL<SampleTestSchema>();
    app.UseGraphQLPlayground(options: new GraphQLPlaygroundOptions());      

    app.UseMvc();
}

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2013-12-07
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多