Registers an IConnectionMultiplexer in the DI container for connecting to Redis®* server. Enables corresponding health check, logging, and telemetry.
- Redis server and the server hostname for connecting a client.
Install the .NET Aspire StackExchange Redis library with NuGet:
dotnet add package Aspire.StackExchange.Redis
In the Program.cs file of your project, call the AddRedisClient
extension method to register an IConnectionMultiplexer
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddRedisClient("cache");
You can then retrieve the IConnectionMultiplexer
instance using dependency injection. For example, to retrieve the connection multiplexer from a Web API controller:
private readonly IConnectionMultiplexer _cache;
public ProductsController(IConnectionMultiplexer cache)
{
_cache = cache;
}
See the StackExchange.Redis documentation for examples on using the IConnectionMultiplexer
.
The .NET Aspire StackExchange Redis component provides multiple options to configure the Redis connection based on the requirements and conventions of your project. Note that at least one host name is required to connect.
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddRedisClient()
:
builder.AddRedisClient("myRedisConnectionName");
And then the connection string will be retrieved from the ConnectionStrings
configuration section:
{
"ConnectionStrings": {
"myRedisConnectionName": "localhost:6379"
}
}
See the Basic Configuration Settings of the StackExchange.Redis docs for more information on how to format this connection string.
The Redis component supports Microsoft.Extensions.Configuration. It loads the StackExchangeRedisSettings
and ConfigurationOptions
from configuration by using the Aspire:StackExchange:Redis
key. Example appsettings.json
that configures some of the options:
{
"Aspire": {
"StackExchange": {
"Redis": {
"ConfigurationOptions": {
"ConnectTimeout": 3000,
"ConnectRetry": 2
},
"DisableHealthChecks": true,
"DisableTracing": false
}
}
}
}
You can also pass the Action<StackExchangeRedisSettings> configureSettings
delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddRedisClient("cache", settings => settings.DisableHealthChecks = true);
You can also setup the ConfigurationOptions using the Action<ConfigurationOptions> configureOptions
delegate parameter of the AddRedisClient
method. For example to set the connection timeout:
builder.AddRedisClient("cache", configureOptions: options => options.ConnectTimeout = 3000);
In your AppHost project, install the Aspire.Hosting.Redis
library with NuGet:
dotnet add package Aspire.Hosting.Redis
Then, in the Program.cs file of AppHost
, register a Redis server and consume the connection using the following methods:
var redis = builder.AddRedis("cache");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(redis);
The WithReference
method configures a connection in the MyService
project named cache
. In the Program.cs file of MyService
, the redis connection can be consumed using:
builder.AddRedisClient("cache");
- https://stackexchange.github.io/StackExchange.Redis/Basics
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
https://github.com/dotnet/aspire
*Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd.