I am trying to connect REDIS database (GCP-memorystore-Redis) from C#. While set values to Redis, I’m getting exception like:
No connection is available to service this operation. UnableToConnect on 10.0.0.2:6379/Interactive, Initializing, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 10s ago, last-write: 10s ago, unanswered-write: 115s ago, keep-alive: 180s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.0.519.65453; IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=1,Free=32766,Min=1,Max=32767), Local-CPU: n/a
I am using StackExchange.Redis version:2.0.519
Code:
IDatabase redisDB;
try {
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect($"{host}:{port},resolvedns=1,abortConnect=False,keepAlive=180,connectTimeout=10000");
redisDB = redis.GetDatabase();
if(redisDB==null)
{
**//Getting Error**
var messageId = redisDB.StreamAdd("event_stream", "foo_name", "bar_value");
//redisDB.StringSet("RedisKey", "RedisValue");
}
}
(or) I am also trying to set values by using below code as well. (getting same issue)
redisDB.StringSet("RedisKey", "RedisValue");
could you please help on this.
2
Answers
It seems that your variable "host" in ConnectionMultiplexer connectionstring isn’t correct. Look at your exception "UnableToConnect on IPAddress:6379/Interactive". So your variable "port" is correct and it has value 6379. Perhaps you have wrong conversion of your "host" variable to string. So you have in it type of variable (IPAddress) instead of real value.
It looks like this might be mostly a formatting/concatenation issue, in which case the simplest approach is: don’t do that. There is a strongly typed object model that makes it very hard to get wrong:
Internally, if you give it a
string
, the first thing it is going to do is parse thestring
to create exactly the above, so you might as well just go direct!