iis - ServiceStack.Redis 在 Windows 服务器的 IIS 上部署时无法连接 : sPort: 0,

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

我正在构建在带有 IIS 的 Windows 服务器上发布的 .Net Core 应用程序后端。在这种情况下,用户可以创建一个 session 并接收发送给它的消息。 session 和消息存储在数据库中,但为了使其更快,我将 Redis 与 PubSub 工具一起使用,该工具处理消息而不是实际对应的 REST 请求。 明确地说,创建 session 时,也会创建对 Redis channel 的订阅,并将消息发布到该 channel ,然后处理程序将消息存储在数据库中。

我使用的是 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)

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

作为this question's answer建议我尝试向 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 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();

关于iis - ServiceStack.Redis 在 Windows 服务器的 IIS 上部署时无法连接 : sPort: 0,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52310273/

相关文章:

Azure 批量应用程序洞察

.net-core - .NET Core 运行时是否向后兼容以前的版本?

mysql - 错误 : 50 - Local Database Runtime error occurred. 在一个站点上有效,但在另一个站点上无效

c# - ASP.NET MVC 重新编译限制达到 15 HostingEnvironment initiated shutdown HostingEnvironment caused shutdown

database - Oracle 数据库更改的 Redis 监听器

python - Tornadio2 + Socket.io 负载测试

node.js - 可以将同一个 Redis 实例与 kue.js 一起手动使用吗?

c# - 某些应用程序是否可能打开/关闭 iis 托管的 Web 服务?

iis - 如何将 IIS 进程 (w3wp.exe) 作为 64 位进程运行

c# - 将多个对象作为参数传递给 mvc 6 操作