0

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,

6
  • You have far too much unnecessary code in your question. All you need to post is the lines with the injection of the IConnectionMultiplexer and the error text and your connection string. Please clean up your question. That said, usually the error connecting right now means 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. Commented Dec 12, 2024 at 16:08
  • hi @mdisibio I have edited the question and kept it precise. Please go through it once from the beginning. You will find it to the point. I don't find the point in downvoting a question though, especially when I am groping around for the source of the error. Commented Dec 12, 2024 at 16:22
  • Better. The error is all about the connection to Redis and not your ResponseCacheService. You hard-coded abortConnect=false but the error seems to think you did not, which suggests that configuration is not getting applied (possibly overwritten by another injection of IConnectionMultiplexer?) 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. Commented Dec 12, 2024 at 17:24
  • Hi @mdisibio Redis was not even installed. I just installed it on my Win10 machine. Given it a username and password. What next? How do I start the redis-server? I tried to run from cmd line --> sudo service redis-server restart but it gives me error --> Failed to restart redis-serve.service: Unit redis-serve.service not found What do I do next? Commented Dec 12, 2024 at 17:43
  • Unfortunately, helping you learn a platform you are completely unfamiliar with is not the purpose of StackOverflow and not in scope of this question. Start here at least, for Redis Commented Dec 12, 2024 at 20:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.