asp.net 核心 2.2 中的响应压缩不起作用

Response compression in asp.net core 2.2 not working

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

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

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

这是我的代码:

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 验证它时,我通过了以下 headers:

Accept-Encoding:br Content-Type:application/json Accept-Language:en-us

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

谁能帮我解决这个问题?

终于解决了。我通过将行 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();
}