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

时间:2018-09-13 09:05:25

标签: iis redis .net-core servicestack servicestack.redis

我正在构建.Net Core应用程序后端,该后端在带有IIS的Windows服务器中发布。在这种情况下,用户可以创建会话并接收消息。会话和消息存储在数据库中,但是为了使其更快,我将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订阅一个频道,并通过后端将消息发送到该频道而没有任何问题。

正如this question's answer所建议的那样,我尝试为Redis Client添加更大的超时,但是仍然失败。

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

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();
            }
        }

    }

谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

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