title | description | ms.date |
---|---|---|
Azure Cache for Redis integration |
Learn how to integrate Azure Cache for Redis with the .NET Aspire stack. |
02/05/2025 |
.NET Aspire Azure Cache for Redis®* integration
[!INCLUDE includes-hosting-and-client]
[!INCLUDE azure-redis-intro]
The .NET Aspire Azure Cache for Redis integration enables you to connect to existing Azure Cache for Redis instances, or create new instances, or run as a container locally from .NET with the docker.io/library/redis
container image.
[!INCLUDE azure-redis-app-host]
[!INCLUDE redis-client-nuget]
In the :::no-loc text="Program.cs"::: file of your client-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireRedisExtensions.AddRedisClient* extension method on any xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder to register an IConnectionMultiplexer
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddRedisClient(connectionName: "cache");
Tip
The connectionName
parameter must match the name used when adding the Azure Cache for Redis resource in the app host project. For more information, see Add Azure Cache for Redis resource.
You can then retrieve the IConnectionMultiplexer
instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(IConnectionMultiplexer connectionMux)
{
// Use connection multiplexer...
}
For more information on dependency injection, see .NET dependency injection.
[!INCLUDE azure-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 xref:Microsoft.Extensions.Hosting.AspireRedisExtensions.AddKeyedRedisClient* method:
builder.AddKeyedRedisClient(name: "chat");
builder.AddKeyedRedisClient(name: "queue");
Then you can retrieve the IConnectionMultiplexer
instances using dependency injection. For example, to retrieve the connection from an example service:
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.
The .NET Aspire Stack Exchange Redis client integration provides multiple options to configure the Redis connection based on the requirements and conventions of your project.
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling xref:Aspire.Hosting.RedisBuilderExtensions.AddRedis*:
builder.AddRedis("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.
[!INCLUDE redis-client-json-settings]
You can also pass the Action<StackExchangeRedisSettings>
delegate to set up some or all the options inline, for example to configure DisableTracing
:
builder.AddRedisClient(
"cache",
static settings => settings.DisableTracing = true);
[!INCLUDE redis-client-health-checks-and-diagnostics]
- Azure Cache for Redis docs
- Stack Exchange Redis docs
- .NET Aspire integrations
- .NET Aspire GitHub repo
[!INCLUDE redis-trademark]