We are trying to implement response compression in ASP.NET Core 6 Web API. We are using, Microsoft.AspNetCore.ResponseCompression -Version 2.2.0
. And here is the code/structure to implement this API:
Program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[]
{
"application/javascript",
"application/json",
"application/xml",
"text/css",
"text/html",
"text/json",
"text/plain",
"text/xml"
});
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
});
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
var app = builder.Build();
app.UseResponseCompression();
This is the sample API:
[Produces("application/json")]
public class ResponseCompressionController: ControllerBase
{
[HttpGet]
public List<Post> Get()
{
List<Post> lstPost = new List<Post>();
for (int count = 0; count < 1000; count++)
{
Post post = new Post();
post.Text = "Adding new post with number" + "_" + count;
lstPost.Add(post);
}
return lstPost;
}
}
However, after doing this, we are not able to see the difference whether the API is sending/compressing the response. Even when we try to keep compression OR not, the response size is same.
We have tried this for other API's but the there is no difference in the response.
Is anything more need to be done?
Any help on this appreciated.
EDIT 1: I have created a new project and kept minimal code, still I am not getting the expected response.
Program.cs
using Microsoft.AspNetCore.ResponseCompression;
using System.IO.Compression;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[]
{
"application/javascript",
"application/json;",
"application/xml",
"text/css",
"text/html",
"text/json",
"text/plain",
"text/xml"
});
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
});
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseResponseCompression();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
And in controller
using Microsoft.AspNetCore.Mvc;
namespace responsecompression.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Interestingly, after applying compression, response size is getting increased.
It seems that Postman is showing the decompressed size not the transferred one, so it is not the ASP.NET Core not working but the observer tool:
Chrome dev tools:
Postman:
Chrome dev tools:
Postman:
Also see: