You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 6中PubNub服务运行数天后停止监听问题求助

ASP.NET Core 6 + PubNub 6.13.0 订阅停止监听问题排查

问题描述

托管在IIS中的ASP.NET Core 6应用,使用PubNub 6.13.0订阅频道并处理消息。服务运行数天后会停止监听PubNub消息,但REST、SignalR等其他功能正常,仅能通过回收应用池恢复PubNub功能。该问题在20台服务器(AWS及本地)上均出现。

当前实现代码

服务注册

services.AddSingleton<IPubNubService, PubNubService>();

启动初始化

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   //...
   app.ApplicationServices.GetService<IPubNubService>(); 
}

PubNubService简化实现

public class PubNubService : IPubNubService
{
    public PubNubService()
    {
        _pubNub = new Pubnub(new PNConfiguration(new UserId($"PubNub:UserId"))
        {
            SubscribeKey = config.GetValue<string>("PubNub:SubscribeKey"), 
            PublishKey = config.GetValue<string>("PubNub:PublishKey"), 
        });
        
        _pubNub.Subscribe<string>()
            .Channels(new[] { "PubNub:BroadcastChannel" })
            .Execute();

        _pubNub.AddListener(CreateListener());
    }

    private SubscribeCallbackExt CreateListener()
    {
        return new SubscribeCallbackExt
        (
            messageCallback: async delegate(Pubnub pnObj, PNMessageResult<object> pubMsg)
            {
                await HandleReceivedMessage(pubMsg);
            },
        );
    }

    private async Task HandleReceivedMessage(PNMessageResult<object> pubNubMessage)
    {
        // ... 消息处理逻辑
    }
}

故障时日志

DateTime 05/30/2023 15:50:55 CheckSocketConnect Entered 
DateTime 05/30/2023 15:50:55, SubscribeManager - ok. expected subscribe within threshold limit of SubscribeTimeout. No action needed 
DateTime 05/30/2023 15:51:05 CheckSocketConnect (HttpClient Or Task.Factory) Failed The request was canceled due to the configured HttpClient.Timeout of 10 seconds elapsing. 
DateTime 05/30/2023 15:51:05 ParseCheckSocketConnectException Error. The request was canceled due to the configured HttpClient.Timeout of 10 seconds elapsing. 
DateTime 05/30/2023 15:53:30 CheckSocketConnect Entered 
DateTime 05/30/2023 15:53:30, SubscribeManager - StartSubscribeHeartbeatCheckCallback - No network or no pubnub instance mapping 
DateTime: 05/30/2023 15:53:30, SendRequestAndGetJsonResponseHttpClient Exception The operation was canceled. 
DateTime 05/30/2023 15:53:30 GetTimeWithHttpClient Resp OK 
DateTime 05/30/2023 15:53:30, reconnection policy is DISABLED, please handle reconnection manually. 
DateTime 05/30/2023 15:56:05 CheckSocketConnect Entered 
DateTime 05/30/2023 15:56:05, SubscribeManager - **No auto subscribe within threshold limit of SubscribeTimeout**. Calling MultiChannelSubscribeRequest 
DateTime 05/30/2023 15:56:05, reconnection policy is DISABLED, please handle reconnection manually.  

核心错误

reconnection policy is DISABLED, please handle reconnection manually

可能原因与解决办法

  • 启用自动重连策略:当前重连策略被禁用是核心问题,在PNConfiguration中开启自动重连:

    var config = new PNConfiguration(new UserId($"PubNub:UserId"))
    {
        SubscribeKey = config.GetValue<string>("PubNub:SubscribeKey"), 
        PublishKey = config.GetValue<string>("PubNub:PublishKey"),
        ReconnectionPolicy = PNReconnectionPolicy.LINEAR // 或EXPONENTIAL,按需选择
    };
    _pubNub = new Pubnub(config);
    
  • 调整初始化顺序:先添加监听器再执行订阅,确保回调注册完成后再接收消息:

    _pubNub.AddListener(CreateListener());
    _pubNub.Subscribe<string>()
        .Channels(new[] { "PubNub:BroadcastChannel" })
        .Execute();
    
  • 添加连接状态监听:在回调中处理连接断开、重连事件,主动干预:

    private SubscribeCallbackExt CreateListener()
    {
        return new SubscribeCallbackExt
        (
            messageCallback: async (pnObj, pubMsg) => await HandleReceivedMessage(pubMsg),
            statusCallback: (pnObj, status) =>
            {
                if (status.Category == PNStatusCategory.PNDisconnectedCategory)
                {
                    // 记录日志并尝试手动重连
                    pnObj.Subscribe<string>().Channels(new[] { "PubNub:BroadcastChannel" }).Execute();
                }
                else if (status.Category == PNStatusCategory.PNReconnectedCategory)
                {
                    // 记录重连成功日志
                }
            }
        );
    }
    
  • 延长超时时间:针对日志中的HttpClient超时问题,调整PubNub配置的超时参数:

    config.SubscribeTimeout = TimeSpan.FromSeconds(30);
    config.NonSubscribeTimeout = TimeSpan.FromSeconds(15);
    
  • 临时恢复方案:若无法立即部署代码修改,可设置IIS应用池定期回收(如每日凌晨),避免服务长时间运行后断开无法恢复。

内容的提问来源于stack exchange,提问作者Eternal21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 02:23:18