Question: How do you solve the error that comes when you are trying to cache an object to Redis and the error says "ArgumentException: No endpoints specified (Parameter 'configuration')".
Login to See the Rest of the Answer
Answer: When working with Redis Docker Container in Asp.Net 5 Application, in order to connect to the external Redis Cache Server you will need to provide the Configuration Settings. The ConfigurationOptions requires the Endpoints to have two parameters, one is for the IP Address (Just IP Address, do not include HTTP or HTTPS) and the other one is for the port. Make sure you put all these settings in the appsettings.json and use the IConfiguration Interface to get the values from the file.
See the code below:
services.AddDistributedMemoryCache();
services.AddStackExchangeRedisCache(options =>
{
options.InstanceName = _configuration.GetValue<string>("NameOfInstanceHere");
options.ConfigurationOptions = new ConfigurationOptions()
{
EndPoints = { { _configuration.GetValue<string>("SettingsBlock:ConnectionString"), PortNumberComesInHere } },
ConnectRetry = 2,
ReconnectRetryPolicy = new LinearRetry(10),
//Ssl = true,
//AbortOnConnectFail = false,
ConnectTimeout = 5000,
//SyncTimeout = 1000,
DefaultDatabase = 0,
Password = _configuration.GetValue<string>("Password")
};
});
To solve the "ArgumentException: No endpoints specified" error when caching an object to Redis, you need to ensure that you provide the necessary configuration settings. Here are the steps:
"Redis": {
"ConnectionString": "localhost:6379"
}
using StackExchange.Redis;
public void ConfigureServices(IServiceCollection services)
{
IConfigurationSection redisConfig = Configuration.GetSection("Redis");
services.AddSingleton<IConnectionMultiplexer>(sp =>
{
string connectionString = redisConfig.GetValue<string>("ConnectionString");
return ConnectionMultiplexer.Connect(connectionString);
});
}
?
IConnectionMultiplexer
instance and accessing the cache through it.By following these steps, you should be able to resolve the "ArgumentException: No endpoints specified" error and successfully cache objects to Redis.