.NET Aspire Stack Exchange Redis integration
In this article, you learn how to use the .NET Aspire Stack Exchange Redis integration. The Aspire.StackExchange.Redis
library is used to register an IConnectionMultiplexer in the DI container for connecting to a Redis server. It enables corresponding health checks, logging and telemetry.
Get started
To get started with the .NET Aspire Stack Exchange Redis integration, install the Aspire.StackExchange.Redis NuGet package in the client-consuming project, i.e., the project for the application that uses the Stack Exchange Redis client.
dotnet add package Aspire.StackExchange.Redis
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Example usage
In the Program.cs file of your client-consuming project, call the AddRedisClient extension to register a IConnectionMultiplexer
for use via the dependency injection container.
builder.AddRedisClient("cache");
You can then retrieve the IConnectionMultiplexer
instance using dependency injection. For example, to retrieve the connection multiplexer from a service:
public class ExampleService(IConnectionMultiplexer connectionMultiplexer)
{
// Use connection multiplexer...
}
App host usage
To model the Redis resource (RedisResource
) in the app host, install the Aspire.Hosting.Redis NuGet package in the app host project.
dotnet add package Aspire.Hosting.Redis
In your app host project, register the .NET Aspire Stack Exchange Redis as a resource using the AddRedis method and consume the service using the following methods:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
builder.AddProject<Projects.ExampleProject>()
.WithReference(cache)
The WithReference method configures a connection in the ExampleProject
project named cache
. In the Program.cs file of ExampleProject
, the Redis connection can be consumed using:
builder.AddRedis("cache");
To model the Garnet resource (GarnetResource
) in the app host, install the Aspire.Hosting.Garnet NuGet package in the app host project.
dotnet add package Aspire.Hosting.Garnet
In your app host project, register .NET Aspire Garnet as a GarnetResource
using the AddGarnet
method and consume the service using the following methods:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddGarnet("cache");
builder.AddProject<Projects.ExampleProject>()
.WithReference(cache)
The WithReference method configures a connection in the ExampleProject
project named cache
. In the Program.cs file of ExampleProject
, the Garnet connection can be consumed using:
builder.AddGarnet("cache");
To model the Valkey resource (ValkeyResource
) in the app host, install the Aspire.Hosting.Valkey NuGet package in the app host project.
dotnet add package Aspire.Hosting.Valkey
In your app host project, register the .NET Aspire Valkey as a resource using the AddValkey
method and consume the service using the following methods:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddValkey("cache");
builder.AddProject<Projects.ExampleProject>()
.WithReference(cache)
The WithReference method configures a connection in the ExampleProject
project named cache
. In the Program.cs file of ExampleProject
, the Valkey connection can be consumed using:
builder.AddValkey("cache");
Configuration
The .NET Aspire Stack Exchange Redis integration provides multiple options to configure the Redis connection based on the requirements and conventions of your project.
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 builder.AddRedis
:
builder.AddRedis("cache");
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddGarnet
:
builder.AddGarnet("cache");
To model the Valkey resource (ValkeyResource
) in the app host, install the Aspire.Hosting.Valkey NuGet package in the app host project.
dotnet add package Aspire.Hosting.Valkey
In your app host project, register the .NET Aspire Valkey as a resource using the AddValkey
method and consume the service using the following methods:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddValkey("cache");
builder.AddProject<Projects.ExampleProject>()
.WithReference(cache)
The WithReference method configures a connection in the ExampleProject
project named cache
. In the Program.cs file of ExampleProject
, the Valkey connection can be consumed using:
builder.AddValkey("cache");
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddValkey
:
builder.AddValkey("cache");
And 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
The .NET Aspire Stack Exchange Redis integration supports Microsoft.Extensions.Configuration. It loads the StackExchangeRedisSettings 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
}
}
}
}
Use inline delegates
You can also pass the Action<StackExchangeRedisSettings>
delegate to set up some or all the options inline, for example to configure DisableTracing
:
builder.AddRedis(
"cache",
settings => settings.DisableTracing = true);
builder.AddGarnet(
"cache",
settings => settings.DisableTracing = true);
To model the Valkey resource (ValkeyResource
) in the app host, install the Aspire.Hosting.Valkey NuGet package in the app host project.
dotnet add package Aspire.Hosting.Valkey
In your app host project, register the .NET Aspire Valkey as a resource using the AddValkey
method and consume the service using the following methods:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddValkey("cache");
builder.AddProject<Projects.ExampleProject>()
.WithReference(cache)
The WithReference method configures a connection in the ExampleProject
project named cache
. In the Program.cs file of ExampleProject
, the Valkey connection can be consumed using:
builder.AddValkey("cache");
builder.AddValkey(
"cache",
settings => settings.DisableTracing = true);
Health checks
By default, .NET Aspire integrations enable health checks for all services. For more information, see .NET Aspire integrations overview.
The .NET Aspire Stack Exchange Redis integration handles the following:
- Adds the
StackExchange.Redis
health check, tries to open the connection and throws when it fails. - Integrates with the
/health
HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic
Observability and telemetry
.NET Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see .NET Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
The .NET Aspire Stack Exchange Redis integration uses the following log categories:
Aspire.StackExchange.Redis
Tracing
The .NET Aspire Stack Exchange Redis integration will emit the following tracing activities using OpenTelemetry:
- "OpenTelemetry.Instrumentation.StackExchangeRedis"
Metrics
The .NET Aspire Stack Exchange Redis integration currently doesn't support metrics by default due to limitations with the StackExchange.Redis
library.