A set of technologies in the .NET Framework for building web applications and XML web services.
Hi @Hoài Nam Nguyễn.
From what I've seen, you’re hitting these problems:
-
localhostis wrong in Aspire when your app runs in a container.
localhost:6379 from inside chat-presense points to the chat-presense container, not the Redis container. In Aspire, when you reference Redis with .WithReference(cache), Aspire injects the Redis connection info into the consuming project (including CACHE_HOST, CACHE_PORT, CACHE_URI, etc.). The Aspire Redis integration docs also explicitly show that WithReference(redis) “Provides ConnectionStrings__cache”. (aspire)
-
AddStackExchangeRedis("cache")does not mean “use the connection string namedcache”.
That overload’s parameter is literally redisConnectionString: String — i.e., it expects the actual Redis connection string, not a key/name to look up in ConnectionStrings. (StackExchangeRedisDependencyInjectionExtensions.AddStackExchangeRedis Method)
This exact mismatch has been reported as a bug/pitfall with Aspire + SignalR backplane: config.GetConnectionString("redis-cache") has the right value at runtime, but passing the name into AddStackExchangeRedis("redis-cache") fails to connect. (Related issue)
To fix this, I'd suggest:
Read the connection string from configuration (which Aspire populates via WithReference(cache)) and pass the resolved value:
var redisCs = builder.Configuration.GetConnectionString("cache");
builder.Services
.AddSignalR()
.AddStackExchangeRedis(redisCs!, options =>
{
// optional: helpful if Redis may start slightly after the app
options.Configuration.AbortOnConnectFail = false;
});
And remove (or don’t use for container runs) the hardcoded localhost setting:
"ConnectionStrings": {
"cache": "localhost:6379,..."
}
Let Aspire supply the correct Redis endpoint on the Aspire network via WithReference(cache) (ex: using the resource hostname like cache + port, not localhost).
Disclaimer: Some links are non-Microsoft website. The pages appear to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classifies as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback. Thank you.