C# StackExchange.Redis client failing to connect

Anonymous
TRIAL

2 years ago

I have deployed a Redis instance and I'm trying to connect to it using StackExchange.Redis but i get the error "It was not possible to connect to the redis server(s)".

using StackExchange.Redis;

namespace Redis
{
    public class Client
    {
        public static readonly ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
            new ConfigurationOptions()
            {
                EndPoints = { "redis://default:password@containers-us-west-174.railway.app:5494" }
            }
        );

       public static double Ping()
       {
           var db = redis.GetDatabase();
           var ping = db.Ping();

           return ping.TotalMicroseconds;
       }
    }
}

but if I connect using the cli it works

0 Replies

Anonymous
TRIAL

2 years ago

0f54028f-2b4f-45ff-85ca-e7f3f2f1a4ea


2 years ago

  1. please use environment variables in your code, never hardcore secrets/credentials

  2. have you setup the redis variable references in your service variables?


Anonymous
TRIAL

2 years ago

  1. I was doing a quick test to see if it would've connected so I didn't bother using env vars

  2. What variables would need to be setup? After I created the service there were already variables


2 years ago

how long ago did you create the service?


Anonymous
TRIAL

2 years ago

5 minutes before I created this post


2 years ago

then you would need to setup the variable references in the service variables tab


2 years ago

scroll down to the reference variables section


Anonymous
TRIAL

2 years ago

If you mean that I need to reference plugin variables in the service env vars then
1) the project is still not hosted on railway so there is no point in doing it and
2) why would that not be a problem when connecting using redis-cli railway_redis_url?


Anonymous
TRIAL

2 years ago

Wdym then?


Anonymous
TRIAL

2 years ago

I have an update


Anonymous
TRIAL

2 years ago

After a bit of experimenting I found out that GetDatabase() needs a database number and that is causing the error


Anonymous
TRIAL

2 years ago

So how do I get the database number?


pierro-mojito
PRO

6 months ago

Hi @iKingNinja, did you find a solution ? I'm not able to connect too