问题描述
我在应用程序中使用ServiceStack.Redis,我的订阅方法是这样:
protected void Subscribe(string channelsToSubscribe)
{
using IRedisClient redisClient = new RedisClient('127.0.0.1','6379');
using var subscription = redisClient.CreateSubscription();
subscription.OnMessage = (channel,msg) =>
{
// do something with the message
};
subscription.Subscribetochannels(channelsToSubscribe);
}
问题:当我尝试订阅频道且Redis脱机时,我需要继续尝试直到连接稳定并正确订阅
所以我做到了:
protected void Subscribe(string channelsToSubscribe)
{
try
{
using IRedisClient redisClient = new RedisClient('127.0.0.1','6379');
using var subscription = redisClient.CreateSubscription();
subscription.OnMessage = (channel,msg) =>
{
// do something with the message
};
subscription.Subscribetochannels(channelsToSubscribe);
}
catch (Exception e)
{
// notify someone that an error happen
Subscribe(channelsToSubscribe);
}
}
这可行,但是如果redis长时间离线,则递归堆栈在充满时会抛出错误(StackOverflowException)。
所以我将递归更改为do while循环:
protected void Subscribe(string channelsToSubscribe)
{
using IRedisClient redisClient = new RedisClient('127.0.0.1','6379');
using var subscription = redisClient.CreateSubscription();
do
{
try
{
subscription.OnMessage = (channel,msg) =>
{
// do something with the message
};
subscription.Subscribetochannels(channelsToSubscribe);
}
catch (Exception e)
{
// notify someone that an error happen
}
} while (redisClient.Ping());
}
但是似乎redisClient.Ping()
也会在Redis断开连接时引发异常,我需要获取一个信息来告诉我Redis是否具有稳定的连接,有人知道解决方案吗? >
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)