我正在使用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(); }
代码构建良好,并使用邮递员对其进行了验证,我传递了以下标头:
Accept-Encoding:br内容类型:application / json接受语言:zh-]
根据上述MSDN文档,服务器应发送标头Content-Encoding:br和Vary:Accept-Encoding。
任何人都可以帮助我解决此问题吗?
我正在使用asp.net core 2.2,Microsoft.EntityFrameworkCore(2.2.4),Microsoft.EntityFrameworkCore.Cosmos(2.2.4),GraphQL.NET开发基于graphql的API。基于链接:https://docs.microsoft ....
最后终于解决了。我通过将以下行移动到app.UseResponseCompression()
来更新了Startup.cs代码中的Configure方法。我验证了它,发现工作正常。