Reputation: 335
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,
Upvotes: 0
Views: 49