title | description | ms.topic | ms.date |
---|---|---|---|
.NET Aspire Stack Exchange Redis component |
This article describes the .NET Aspire Stack Exchange Redis component features and capabilities |
how-to |
06/11/2024 |
In this article, you learn how to use the .NET Aspire Stack Exchange Redis component. 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.
To get started with the .NET Aspire Stack Exchange Redis component, install the Aspire.StackExchange.Redis NuGet package.
dotnet add package Aspire.StackExchange.Redis
<PackageReference Include="Aspire.StackExchange.Redis"
Version="[SelectVersion]" />
For more information, see dotnet add package or Manage package dependencies in .NET applications.
In the :::no-loc text="Program.cs"::: file of your component-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireRedisExtensions.AddRedisClient%2A 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...
}
[!INCLUDE redis-app-host]
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddRedis("redis");
builder.AddProject<Projects.ExampleProject>()
.WithReference(redis)
The xref:Aspire.Hosting.ResourceBuilderExtensions.WithReference%2A method configures a connection in the ExampleProject
project named redis
. In the :::no-loc text="Program.cs"::: file of ExampleProject
, the Redis connection can be consumed using:
builder.AddRedis("cache");
The .NET Aspire Stack Exchange Redis component 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 builder.AddRedis
:
builder.AddRedis("RedisConnection");
And then the connection string will be retrieved from the ConnectionStrings
configuration section:
{
"ConnectionStrings": {
"RedisConnection": "localhost:6379"
}
}
For more information on how to format this connection string, see the Stack Exchange Redis configuration docs.
The .NET Aspire Stack Exchange Redis component supports xref:Microsoft.Extensions.Configuration?displayProperty=fullName. It loads the xref:Aspire.StackExchange.Redis.StackExchangeRedisSettings from configuration by using the Aspire:StackExchange:Redis
key. Example :::no-loc text="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>
delegate to set up some or all the options inline, for example to configure DisableTracing
:
builder.AddRedis(
"cache",
settings => settings.DisableTracing = true);
[!INCLUDE component-health-checks]
The .NET Aspire Stack Exchange Redis component 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
[!INCLUDE component-observability-and-telemetry]
The .NET Aspire Stack Exchange Redis component uses the following log categories:
Aspire.StackExchange.Redis
The .NET Aspire Stack Exchange Redis component will emit the following tracing activities using OpenTelemetry:
- "OpenTelemetry.Instrumentation.StackExchangeRedis"
The .NET Aspire Stack Exchange Redis component currently doesn't support metrics by default due to limitations with the StackExchange.Redis
library.