1

I am using asp.net core 2.2, Microsoft.EntityFrameworkCore(2.2.4), Microsoft.EntityFrameworkCore.Cosmos(2.2.4), GraphQL.NET to develop graphql based APIs.

Based on the link : https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0

I configured the settings in the Startup.cs class methods to enable Compression.

Here goes my code:

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();
}

The code is building fine and on validating it using Postman I passed the below headers:

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

As per the MSDN document mentioned above the server should sent the header Content-Encoding:br and Vary:Accept-Encoding.

Can anyone help me to fix this issue?

BurnsBA
  • 4,347
  • 27
  • 39
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • I *think* you still need to add Providers in your `services.AddResponseCompression` setup: `services.AddResponseCompression(options => { options.Providers.Add(); options.Providers.Add(); });` (but I'm not sure, or I'd post this as an answer) – BurnsBA Nov 01 '19 at 13:16
  • As per the MSDN link: https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0 they are available as default. I did another POC and found working but the code posted above is not working for me. – santosh kumar patro Nov 01 '19 at 13:21
  • What server are you using? The link you provided states that neither Kestrel nor Http.sys offer built-in compression support. – ckuri Nov 01 '19 at 15:10
  • Hence the reason I am using the inbuilt middleware provided by asp.net core to perform the compression. – santosh kumar patro Nov 01 '19 at 16:42
  • One small update , I am using Azure Dev Spaces to run the project and in the response I am seeing Server: Kestrel, Content-Type:application/json, Transfer-Encoding:chunked – santosh kumar patro Nov 01 '19 at 17:45

1 Answers1

4

Finally it got resolved. I updated the Configure method in the Startup.cs code by moving the line: app.UseResponseCompression() to the top of the method. I validated it and found working fine as expected.

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();
}
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143