使用StackExchange库在Windows上从C#连接到redis时出错

时间:2017-04-29 17:29:49

标签: c# windows redis

我尝试使用StackExchange library在C#上连接到Windows上的Redis,但收到错误:

  

StackExchange.Redis.RedisConnectionException:没有连接   可用于此操作:HMSET用户:user1; SocketFailure   on localhost:6379 / Subscription,origin:错误,输入缓冲区:0,   优秀:0,最后读:0s前,最后写:0s前,   unanswered-write:7979s ago,keep-alive:60s,pending:0,state:   连接,last-heartbeat:never,last-mbeat:-1s ago,global:1s ago

我的代码看起来像这样:

class RedisConnectionFactory
{
    private static readonly Lazy<ConnectionMultiplexer> Connection;

    private static Lazy<ConfigurationOptions> configurationOptions = new Lazy<ConfigurationOptions>(() =>
    {
        var configurationOptions = new ConfigurationOptions();
        configurationOptions.EndPoints.Add("127.0.0.1", 6379);
        configurationOptions.Password = "<PASS>";
        configurationOptions.ClientName = "JakubStompor";
        configurationOptions.AbortOnConnectFail = false;
        configurationOptions.Ssl = false;
        configurationOptions.ConnectTimeout = 1000;
        configurationOptions.SyncTimeout = 3000;
        return configurationOptions;
    });

    static RedisConnectionFactory()
    {
        Connection = new Lazy<ConnectionMultiplexer>(
            () => ConnectionMultiplexer.Connect(configurationOptions.Value)
        );
    }
    public static ConnectionMultiplexer GetConnection() => Connection.Value;
}

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        var conn = RedisConnectionFactory.GetConnection();
        IDatabase db = conn.GetDatabase();
        db.HashSet("user:user1", new HashEntry[] { new HashEntry("12", "13"), new HashEntry("14", "15") });
    }
}

我已经运行了redis服务器,我可以使用命令行进行连接。

1 个答案:

答案 0 :(得分:0)

您的ConnectTimeout可能太低了吗?您可以尝试将其增加到5000或更多以查看它是否有帮助吗?

configurationOptions.ConnectTimeout = 1000;