Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dotnet/aspire/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheAspire.StackExchange.Redis component registers an IConnectionMultiplexer in your dependency injection container for connecting to Redis servers. It automatically enables health checks, logging, and distributed tracing.
Installation
Install the component using the .NET CLI:Usage
Register the component
In your service’sProgram.cs file, call the AddRedisClient extension method:
Inject and use the connection multiplexer
Retrieve theIConnectionMultiplexer instance using dependency injection:
Configuration
The component provides multiple configuration options based on your project requirements.Connection string
Provide a connection string in yourappsettings.json:
Configuration providers
Configure component settings using theAspire:StackExchange:Redis configuration section:
Inline configuration
Configure settings directly in code using a delegate:ConfigurationOptions:
AppHost integration
In your AppHost project, install the Redis hosting package:Program.cs:
WithReference method automatically configures the connection string in your service. Consume it in your service’s Program.cs:
The connection name passed to
WithReference must match the name used in AddRedisClient.Configuration options
The following settings are available:| Setting | Description | Default |
|---|---|---|
DisableHealthChecks | Disable automatic health check registration | false |
DisableTracing | Disable OpenTelemetry tracing | false |
ConfigurationOptions | Configure StackExchange.Redis options | null |
Health checks
The component automatically registers a health check that verifies the Redis connection. The health check:- Pings the Redis server
- Verifies connectivity
- Reports the status in the
/healthendpoint
DisableHealthChecks to true.
Observability
Logging
The component integrates with .NET logging to provide:- Connection events
- Command execution information
- Error details
Tracing
Distributed tracing captures:- Redis operations
- Command execution time
- Connection activity
Common scenarios
Caching
Use Redis as a distributed cache:Pub/Sub messaging
Use Redis for publish/subscribe messaging:Distributed locks
Implement distributed locks:Session storage
Store session data in Redis:Best practices
Reuse the connection multiplexer
Reuse the connection multiplexer
The
IConnectionMultiplexer is designed to be reused throughout your application. Never create multiple instances - use dependency injection to share a single instance.Use appropriate data types
Use appropriate data types
Redis supports multiple data types (strings, hashes, lists, sets, sorted sets). Choose the appropriate type for your use case:
Set appropriate expiration times
Set appropriate expiration times
Always set expiration times for cache entries to prevent memory exhaustion:
Handle connection failures gracefully
Handle connection failures gracefully
Implement fallback logic for when Redis is unavailable:
Use batching for multiple operations
Use batching for multiple operations
Use batching to reduce round trips:
Additional resources
Related components
PostgreSQL
Connect to PostgreSQL databases
MongoDB
Work with MongoDB databases
Azure Cosmos DB
Connect to Azure Cosmos DB
Redis Hosting
Learn about the Redis hosting integration