I am facing an exception(ConnectionMultiplexer) message when trying to access a .NetCore 8 webAPI endpoint. I tried to use repositoryPattern in my project so there's a GenericRepository interface. I registered my dependencies in a separate class and then added it to my Program.cs class using Services.Add().Let me post the relevant pieces of code so that the structure is understandable to you. There's a whole lot of inter-related code so pls bear with me and pardon me if it appears too verbose.
**** I have removed the unrelated code and edited my question for the sake of brevity and clarity.
The StackExchange.Redis exception I am getting is
HirectStore.API.Infrastructure.Services.ResponseCacheService.GetCachedResponseAsync(String cacheKey) in D:\Code Stack\Projects\HirectStore\HirectStoreBackend\HirectStore.API\Infrastructure\Services\ResponseCacheService.cs:line 35\r\n
In my Program.cs -
builder.Services.AddSingleton<IConnectionMultiplexer>(c => { var multiplexerconfig = ConfigurationOptions.Parse("localhost, abortConnect=false"); return ConnectionMultiplexer.Connect(multiplexerconfig); }); builder.Services.AddApplicationServices(); I inspected the exception in my ExceptionMiddleware class and it says the following -
'It was not possible to connect to the redis server(s). Error connecting right now. To allow this multiplexer to continue retrying until it's able to connect, use abortConnect=false in your connection string or AbortOnConnectFail=false; in your code.'
This is how I registered the dependencies of ResponseCache -
public static IServiceCollection AddApplicationServices(this IServiceCollection services) { services.AddSingleton<IResponseCacheService, ResponseCacheService>(); return services; } IResponseCacheService.cs -
public interface IResponseCacheService { Task CacheResponseAsync(string cacheKey, object response, TimeSpan timeToLive); Task<string> GetCachedResponseAsync(string cacheKey); } and it's implementation -
public class ResponseCacheService:IResponseCacheService { private readonly IDatabase _database; public ResponseCacheService(IConnectionMultiplexer redis) { _database = redis.GetDatabase(); } public async Task CacheResponseAsync(string cacheKey, object response, TimeSpan timeToLive) { if (response == null) { return; }; var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var serialisedResponse = JsonSerializer.Serialize(response, options); await _database.StringSetAsync(cacheKey, serialisedResponse, timeToLive); } public async Task<string> GetCachedResponseAsync(string cacheKey) { ***exception here*** var cachedResponse = await _database.StringGetAsync(cacheKey); if (cachedResponse.IsNullOrEmpty) { return null; } return cachedResponse; } } So where's the twist? What is this exception actually trying to convey? In all the code I posted, what exactly is causing the above exception? I am not really sure about the purpose of the ConnectionMultiplexer, I followed it from a tutorial. Even that tutorial didn't provide a thorough explanation. How should I remove this exception and get my webAPI working? Need your kind assistance on this.
Thanks,
IConnectionMultiplexerand the error text and your connection string. Please clean up your question. That said, usually theerror connecting right nowmeans just that. It is not finding the Redis server. Check it is running, and that the port is either default or specified in the connection string. Hard code the connection string first, then check if the code is reading your config correctly.abortConnect=falsebut the error seems to think you did not, which suggests that configuration is not getting applied (possibly overwritten by another injection ofIConnectionMultiplexer?) Check Redis is really up and running locally on port 6379 and you can access it from a command line or small Console App test without DI. If you are running in WSL, you might try using 127.0.0.1 instead of localhost. If hosted in a docker container, ensure the port is forwarded.