ServiceStack.Redis 无法连接:sPort:0,在 Windows Server 中的 IIS 上部署时



我正在构建在带有IIS的Windows服务器中发布的.Net Core应用程序后端。在此方案中,用户可以创建会话并向其接收消息。会话和消息存储在数据库中,但为了更快,我将 Redis 与 PubSub 工具一起使用,该工具处理消息而不是实际的相应 REST 请求。 明确地说,创建会话时,还会创建对 Redis 通道的订阅,并将消息发布到该通道,然后处理程序将消息存储在数据库中。

我使用的是ServiceStack.Redis的许可版本,并且在Visual Studio(Kestrel(的开发服务器中一切正常,但是一旦我将其部署到IIS,大多数时候都会收到此错误:

Unable to Connect: sPort: 0, Error: Object reference not set to an instance of an object.
at ServiceStack.Redis.RedisNativeClient.Connect()
at ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)

这有点随机,因为也许可以订阅几个频道,然后它又开始失败一段时间,或者从一开始就失败了。服务器的防火墙被禁用,Redis 也已通过 msi 安装程序作为服务安装,默认配置和 redis-cli 工作正常。实际上,我可以从 redis-cli 订阅一个频道,并通过后端将消息发送到该频道,没有任何问题。

正如这个问题的答案所表明的那样,我试图向 Redis 客户端添加更大的超时,但仍然失败了。

这是代码,如果有帮助的话:

private void CreateRedisSubscription(string channelName)
{
_log.LogInformation("Creating subscription to channel " + channelName);
using (RedisClient redisClient = new RedisClient(Globals.REDIS_HOST, Globals.REDIS_PORT)){
// Too big timeout to make sure the error it's not because of a timeout.
redisClient.ConnectTimeout = 30000;
using (subscription = redisClient.CreateSubscription())
{
subscription.OnSubscribe = channel =>
{
Console.Write("Subscribed to channel: " + channel);
};
subscription.OnUnSubscribe = channel =>
{
Console.Write("Unsubscribed from channel: " + channel);
ThreadsMember threadMember = subscriptionThreads.Find(t => t.ThreadName.Equals(channel));
if(threadMember != null)
{
threadMember.subscriptionThread.Abort();
subscriptionThreads.Remove(threadMember);
}
};
subscription.OnMessage += (channel, message) => ProcessMessage(channel, message);
Thread thread = new Thread(() =>
{
try{
subscription.SubscribeToChannels(channelName);
_log.LogInformation("Subscribed to channel " + channelName);
}catch (Exception e)
{
Console.WriteLine(e.Message);
_log.LogWarning("Can not subscribe to channel " + channelName);
_log.LogError(e.Message);
_log.LogTrace(e.StackTrace);
if(e.InnerException != null)
_log.LogError(e.InnerException.Message);                                    
}            
});
thread.IsBackground = true;
//Add the thread to a list for future management (unsubscribe from that channel)
subscriptionThreads.Add(new ThreadsMember(channelName, thread));
thread.Start();
}
}
}

提前谢谢。

应避免使用Thread.Abort(),因为它会使中止的实例处于不一致状态。

我建议改用 ServiceStack.Redis 托管订阅,它负责管理订阅,并允许您实现回调来处理不同的订阅事件:

var clientsManager = new PooledRedisClientManager();
var redisPubSub = new RedisPubSubServer(clientsManager, "channel-1", "channel-2") {
OnMessage = (channel, msg) => "Received '{0}' from '{1}'".Print(msg, channel)
}.Start();

最新更新