C# StackExchange.Redis client failing to connect

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;
}
}
}
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
8 Replies
Percy
Percy15mo ago
Project ID: 0f54028f-2b4f-45ff-85ca-e7f3f2f1a4ea
iKingNinja
iKingNinja15mo ago
0f54028f-2b4f-45ff-85ca-e7f3f2f1a4ea
Brody
Brody15mo 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?
iKingNinja
iKingNinja15mo 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
Brody
Brody15mo ago
how long ago did you create the service?
iKingNinja
iKingNinja15mo ago
5 minutes before I created this post
Brody
Brody15mo ago
then you would need to setup the variable references in the service variables tab https://docs.railway.app/develop/variables scroll down to the reference variables section
iKingNinja
iKingNinja15mo 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? Wdym then? I have an update After a bit of experimenting I found out that GetDatabase() needs a database number and that is causing the error So how do I get the database number?