If you are running an ASP.NET Core application, there is a specific package that makes StackExchange.Redis.Extensions configuration easier.
dotnet add package StackExchange.Redis.Extensions.AspNetCore<PackageReference Include="StackExchange.Redis.Extensions.AspNetCore" Version="12.*" />Register the library in your Program.cs using the minimal hosting model:
var builder = WebApplication.CreateBuilder(args);
var redisConfiguration = builder.Configuration
.GetSection("Redis")
.Get<RedisConfiguration>();
builder.Services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(redisConfiguration);
var app = builder.Build();
app.UseRedisInformation();
app.Run();{% hint style="info" %}
Remember to install your preferred serializer package as well (e.g., StackExchange.Redis.Extensions.System.Text.Json).
{% endhint %}
You can optionally enable transparent compression by calling AddRedisCompression right after AddStackExchangeRedisExtensions:
builder.Services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(redisConfiguration);
builder.Services.AddRedisCompression<LZ4Compressor>();See the Compression page for all available compressors and configuration options.
Monitor the Redis connection pool status through ASP.NET Core health checks:
builder.Services.AddHealthChecks()
.AddRedisExtensionsHealthCheck();Returns Healthy / Degraded / Unhealthy based on pool state and PING result. See Health Check for details.
Use the standard IDistributedCache abstraction backed by the same Redis connection:
builder.Services.AddRedisDistributedCache();See IDistributedCache Adapter for sliding expiration behavior and storage format.
Once registered, you can inject IRedisDatabase directly into your controllers, services, or minimal API handlers:
app.MapGet("/users/{key}", async (string key, IRedisDatabase redis) =>
{
var user = await redis.GetAsync<User>(key);
return user is not null ? Results.Ok(user) : Results.NotFound();
});