Redis client integration
Это содержимое пока не доступно на вашем языке.
To get started with the Aspire Redis client integration, install the 📦 Aspire.StackExchange.Redis NuGet package:
dotnet add package Aspire.StackExchange.Redis#:package Aspire.StackExchange.Redis@*<PackageReference Include="Aspire.StackExchange.Redis" Version="*" />Add Redis client
Section titled “Add Redis client”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...}Add keyed Redis client
Section titled “Add keyed Redis client”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.
Connection properties
Section titled “Connection properties”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 Name | Description |
|---|---|
Host | The hostname or IP address of the Redis server |
Port | The port number the Redis server is listening on |
Password | The password for authentication |
Uri | The connection URI, with the format redis://:{Password}@{Host}:{Port} |
Example connection string:
Uri: redis://:p%40ssw0rd1@localhost:6379Redis client builder pattern
Section titled “Redis client builder pattern”The Redis client builder pattern provides a fluent, type-safe approach to configuring Redis clients with integrated support for distributed caching and Azure authentication.
Basic usage
Section titled “Basic usage”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.
Azure authentication
Section titled “Azure authentication”To enable Azure authentication for Redis, add a reference to the 📦 Aspire.Microsoft.Azure.StackExchangeRedis NuGet package:
dotnet add package Aspire.Microsoft.Azure.StackExchangeRedis#:package Aspire.Microsoft.Azure.StackExchangeRedis@*<PackageReference Include="Aspire.Microsoft.Azure.StackExchangeRedis" Version="*" />Then chain a call to WithAzureAuthentication():
builder.AddRedisClientBuilder("cache") .WithAzureAuthentication() .WithDistributedCache(options => { options.InstanceName = "MyApp"; });Auto activation
Section titled “Auto activation”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 falsebuilder.AddRedisClient("cache", c => c.DisableAutoActivation = false);Configuration
Section titled “Configuration”Use a connection string
Section titled “Use a connection string”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.
Use configuration providers
Section titled “Use configuration providers”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 } } }}Use inline delegates
Section titled “Use inline delegates”You can pass the delegate to set up options inline:
builder.AddRedisClient( "cache", static settings => settings.DisableTracing = true);Client integration health checks
Section titled “Client integration health checks”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.
Observability and telemetry
Section titled “Observability and telemetry”Logging
Section titled “Logging”The Redis integration uses the following log categories:
Aspire.StackExchange.Redis
Tracing
Section titled “Tracing”The Redis integration will emit the following tracing activities using OpenTelemetry:
OpenTelemetry.Instrumentation.StackExchangeRedis
Metrics
Section titled “Metrics”The Redis integration emits metrics using OpenTelemetry.