Pular para o conteúdo

Redis client integration

Este conteúdo não está disponível em sua língua ainda.

To get started with the Aspire Redis client integration, install the 📦 Aspire.StackExchange.Redis NuGet package:

.NET CLI — Add Aspire.StackExchange.Redis package
dotnet add package Aspire.StackExchange.Redis

In the Program.cs file of your client-consuming project, call the AddRedisClient extension method to register an IConnectionMultiplexer for use via the dependency injection container:

builder.AddRedisClient(connectionName: "cache");

You can then retrieve the IConnectionMultiplexer instance using dependency injection:

public class ExampleService(IConnectionMultiplexer connectionMux)
{
// Use connection multiplexer...
}

There might be situations where you want to register multiple IConnectionMultiplexer instances with different connection names. To register keyed Redis clients, call the AddKeyedRedisClient method:

builder.AddKeyedRedisClient(name: "chat");
builder.AddKeyedRedisClient(name: "queue");

Then retrieve the instances:

public class ExampleService(
[FromKeyedServices("chat")] IConnectionMultiplexer chatConnectionMux,
[FromKeyedServices("queue")] IConnectionMultiplexer queueConnectionMux)
{
// Use connections...
}

For more information on keyed services, see .NET dependency injection: Keyed services.

When you reference a Redis resource using WithReference, the following connection properties are made available to the consuming project:

The Redis resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Redis server
PortThe port number the Redis server is listening on
PasswordThe password for authentication
UriThe connection URI, with the format redis://:{Password}@{Host}:{Port}

Example connection string:

Uri: redis://:p%40ssw0rd1@localhost:6379

The Redis client builder pattern provides a fluent, type-safe approach to configuring Redis clients with integrated support for distributed caching and Azure authentication.

Use AddRedisClientBuilder to configure Redis clients with a fluent API:

var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClientBuilder("cache")
.WithDistributedCache(options =>
{
options.InstanceName = "MyApp";
});

The client builder pattern simplifies the configuration of Redis-backed caching services by combining client setup with caching configuration in a single fluent chain.

To enable Azure authentication for Redis, add a reference to the 📦 Aspire.Microsoft.Azure.StackExchangeRedis NuGet package:

.NET CLI — Add Aspire.Microsoft.Azure.StackExchangeRedis package
dotnet add package Aspire.Microsoft.Azure.StackExchangeRedis

Then chain a call to WithAzureAuthentication():

builder.AddRedisClientBuilder("cache")
.WithAzureAuthentication()
.WithDistributedCache(options =>
{
options.InstanceName = "MyApp";
});

Redis client connections support auto activation to prevent startup deadlocks and improve application reliability. Auto activation is disabled by default but can be enabled using the DisableAutoActivation option:

var builder = WebApplication.CreateBuilder(args);
// Enable auto activation by setting DisableAutoActivation to false
builder.AddRedisClient("cache", c => c.DisableAutoActivation = false);

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling AddRedisClient:

builder.AddRedisClient("cache");

Then the connection string will be retrieved from the ConnectionStrings configuration section:

{
"ConnectionStrings": {
"cache": "localhost:6379"
}
}

For more information on how to format this connection string, see the Stack Exchange Redis configuration docs.

The Redis client integration supports Microsoft.Extensions.Configuration. It loads the StackExchangeRedisSettings from configuration using the Aspire:StackExchange:Redis key. Example appsettings.json:

{
"Aspire": {
"StackExchange": {
"Redis": {
"ConnectionString": "localhost:6379",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}

You can pass the delegate to set up options inline:

builder.AddRedisClient(
"cache",
static settings => settings.DisableTracing = true);

By default, Aspire integrations enable health checks for all services. The Redis integration adds a health check that verifies the Redis instance is reachable and can execute commands.

The Redis integration uses the following log categories:

  • Aspire.StackExchange.Redis

The Redis integration will emit the following tracing activities using OpenTelemetry:

  • OpenTelemetry.Instrumentation.StackExchangeRedis

The Redis integration emits metrics using OpenTelemetry.