The goal
By the end you'll have a Redis container running as a managed resource in the Aspire dashboard, and a service reading and writing through an injected IDistributedCache.
This is the same shape as wiring up a database with the SQL Server resource: the cache is a managed resource - a container Aspire owns - and the consuming project never sees a hand-written connection string. If you want the machinery underneath AddRedis and WithReference, that is Inside Aspire's AppHost.
Prerequisites
aspire --versionaspire run before and know your way around the dashboardSteps
Add the Redis hosting integration to the AppHost
ExpectedAspire.Hosting.Redis is added to AppHost.csproj; AddRedis and RedisResource are now in scope.
Model a Redis resource and wire it to your service
Declare the container in the AppHost and reference it from the project that needs it. WaitFor holds the service until Redis passes its health check.
ExpectedThe solution builds. At run time the api project receives a ConnectionStrings__cache environment variable.
Add the caching client integration to your service
This goes in the consuming project, not the AppHost. It brings the IDistributedCache wiring backed by StackExchange.Redis.
ExpectedAspire.StackExchange.Redis.DistributedCaching is added to Api.csproj; AddRedisDistributedCache is in scope.
Register the distributed cache
The name must match the resource name from step 2 - that is how Aspire matches the injected connection string.
ExpectedIDistributedCache resolves from DI; there is no localhost:6379 string anywhere in your code or config.
Cache a response, then run it
Read through the cache first; compute and write back only on a miss. Then start the whole topology with the Aspire CLI.
ExpectedRun aspire run. The dashboard opens; the first GET /forecast computes, a second within 5 minutes returns instantly from Redis.
Verify
You're done when every one of these holds:
- The dashboard lists a cache resource of type Container, state Running.
-
The cache resource logs show Redis
Ready to accept connections. -
The api resource's environment shows
ConnectionStrings__cacheinjected by Aspire. -
First
GET /forecastis a miss (it computes); the next is served from cache, visibly faster. -
The
forecastkey exists in Redis - confirm via the dashboard's resource commands orredis-cli KEYS *.
Troubleshooting
SymptomStartup throws Unable to resolve service for type 'IDistributedCache'.
FixYou added the hosting package to the AppHost but not the client package to the service. Install Aspire.StackExchange.Redis.DistributedCaching in the consuming project and call AddRedisDistributedCache("cache") there.
SymptomThe service starts before Redis and the first calls fail with a connection refused.
FixAdd .WaitFor(cache) to the project in the AppHost so it waits for the container's health check before launching.
SymptomThe cache resource is stuck on Starting, or the image pull fails.
FixYour container runtime isn't running. Start Docker Desktop or Podman, then re-run aspire run - Aspire pulls docker.io/library/redis on first launch.
References
- Get started with the Redis integrationsaspire.dev/integrations/caching/redis
- Aspire Redis integration - AddRedis and the hosting packagelearn.microsoft.com/dotnet/aspire/caching
- Redis distributed caching - AddRedisDistributedCacheaspire.dev/integrations/caching/redis-distributed
- Aspire.StackExchange.Redis.DistributedCaching on NuGetnuget.org/packages/Aspire.StackExchange.Redis.DistributedCaching
- Inside Aspire's AppHoststacknova · engineering · apphost