简体   繁体   English

StackExchange.Redis:没有可用的连接来服务这个操作。 UnableToConnect on HOST:PORT/Interactive

[英]StackExchange.Redis: No connection is available to service this operation. UnableToConnect on HOST:PORT/Interactive

I am trying to connect REDIS database (GCP-memorystore-Redis) from C#.我正在尝试从 C# 连接 REDIS 数据库(GCP-memorystore-Redis)。 While set values to Redis, I'm getting exception like:在将值设置为 Redis 时,我收到如下异常:

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 ; UnableToConnect on 10.0.0.2 : 6379 / Interactive , Initializing, last: NONE, origin: BeginConnectAsync,outstanding: 0, last-read: 10s ago, last-write: 10s ago, unanswer-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 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我使用StackExchange.Redis版本: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.你能帮忙解决这个问题吗?

It seems that your variable "host" in ConnectionMultiplexer connectionstring isn't correct. ConnectionMultiplexer connectionstring 中的变量“host”似乎不正确。 Look at your exception "UnableToConnect on IPAddress:6379/Interactive".查看您的异常“IPAddress:6379/Interactive 上的 UnableToConnect”。 So your variable "port" is correct and it has value 6379. Perhaps you have wrong conversion of your "host" variable to string.所以你的变量“端口”是正确的,它的值为 6379。也许你将“主机”变量转换为字符串是错误的。 So you have in it type of variable (IPAddress) instead of real value.所以你有变量类型(IPAddress)而不是实际值。

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:有一个强类型对象模型,它很难出错:

var config = new ConfigurationOptions {
    EndPoints = {
        new DnsEndPoint(host, port),
        // new IPEndPoint(host, port), // <== or this if 'host' is an IPAddress
    },
    ResolveDns = true,
    AbortOnConnectFail = false,
    KeepAlive = 180,
    ConnectTimeout = 10000,
};
var muxer = ConnectionMultiplexer.Connect(config);

Internally, if you give it a string , the first thing it is going to do is parse the string to create exactly the above, so you might as well just go direct!在内部,如果你给它一个string ,它要做的第一件事就是解析string以创建上面的内容,所以你不妨直接去!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM