StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.84k stars 1.5k forks source link

Not sure what I'm doing wrong, trying to connect to redis, "The specified host and port could not be parsed" #2671

Closed push-22 closed 3 months ago

push-22 commented 3 months ago

Hi,

I'm on a mac laptop, which is running redis/redis-stack under docker desktop, on the same laptop I'm also running Windows 11 inside VMWare fusion.

From the windows, I can connect to the redis-stack browser ok, but from code (c#) if I try to connect I get the exception: Unhandled exception. System.ArgumentException: The specified host and port could not be parsed: 192.164.14.220:6379,abortConnect=True (Parameter 'hostAndPort') at StackExchange.Redis.ConnectionMultiplexer.GetServer(String hostAndPort, Object asyncState) in //src/StackExchange.Redis/ConnectionMultiplexer.cs:line 1224 at SM3.Shared.ConnectionData.SMSubscriberCount(IConnectionMultiplexer muxer) in C:\Pub-Sub-Demo\common\ConnectionData.cs:line 69 at Program.<>c__DisplayClass0_0.<<

$>b_0>d.MoveNext() in C:\Pub-Sub-Demo\servicemonitoring\Program.cs:line 25

My code is very simple:

ConnectionMultiplexer.Connect($"{host}:{port}", (cfg) => { 
      cfg.AbortOnConnectFail = true;
       cfg.ReconnectRetryPolicy = new LinearRetry(5000);
}, logger);

If I change the code to

ConnectionMultiplexer.Connect($"{host}:{port}", logger);

Then everything works as expected.

This also fails:

host = '192.164.14.220:' / port = standard

      var configuration = new ConfigurationOptions() {
          //for the redis pool so you can extent later if needed
          EndPoints = { { host, port }, },
          AllowAdmin = true,
          //Password = "", //to the security for the production
          //ClientName = "My Redis Client",
          //ReconnectRetryPolicy = new LinearRetry(5000),
          //AbortOnConnectFail = false,
      };

But when I comment out the AllowAdmin it works again.

Not sure what i'm doing wrong here, it appears to be trying to parse the cfg json and combining both the Endpoints data and attributes together.

stackexchange.redis: 2.7.27 docker container is running Redis version=7.2.3, bits=64

NickCraver commented 3 months ago

Looking at the stack and lines, it doesn't appear connecting is your issue, nor can I repro any issue. It looks like you have a GetServer() call somewhere further down doing this, and you'd need to look at that calling code.

push-22 commented 3 months ago

yup, you're right, tracked it down to an extension method

    public static (long pCount, long sCount) SMSubscriberCount(this IConnectionMultiplexer muxer) {
        var svr = muxer.GetServer(muxer.Configuration);
        return (svr.SubscriptionPatternCount(), svr.SubscriptionSubscriberCount(SubscriptionData.SubscriptionAllChannel));
    }