Task.Run 内部后台服务意外停止工作

Task.Run inside backgroundservice unexpectedly stops working

我在后台服务中使用 Task.Run。我有后台服务,负责特定系统的登录和发送心跳。

public class TestBGService : IHostedService
{
    private readonly ITestService _testService;
    private bool MustLogOn { get; set; } = true;
    private string id { get; set; }
    public TestBGService(ITestService testService)
    {
        _testService = testService;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            Log.Information("TestBGService starts Initialize");
            await Initialize();
        }
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }

    private async Task Initialize()
    {
        try
        {
            if (MustLogOn)
            {
                Log.Information("TestBGService ExecuteAsync trying to LogOnAsync");
                id = await _testService.LogOnAsync();
                if (!string.IsNullOrEmpty(id))
                {
                    Log.Information($"new id equals to {id}");
                    MustLogOn = false;

                    _ = Task.Run(async () =>
                     {
                         while (true)
                         {
                             bool res = await SendHeartBeat(id);
                             Log.Information($"res from SendHeartBeat {res}");
                             if (!res)
                             {
                                 break;
                             }
                             await Task.Delay(10000);
                         }
                     });
                    await _testService.StartProcessAsync(id);
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error($"TestBGService ExecuteAsync throws {ex.ToString()}");
        }
    }


    private async Task<bool> SendHeartBeat(string id)
    {
        bool isSuccess = true;
        try
        {
            Log.Information("TestBGService sending heartbeat at " + DateTime.Now);
            var response = new HeartBeatResponseModel();
            response = await _testService.SendHeartBeatAsync(id);
            Log.Information("TestBGService heartbeat response equals to " + response.IsSuccessful);
            if (!response.IsSuccessful)
            {
                MustLogOn = true;
                isSuccess = response.IsSuccessful;
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex, "TestBGService SendHeartBeat throws");
            isSuccess = false;
            MustLogOn = true;
        }
        return isSuccess;
    }

}

尝试登录系统的初始化方法,成功后需要启动SendHeartBeat。 SendHeartBeat 方法负责获取成功或失败。如果成功,我将 MustLogon 的值更改为 false 并每 10 秒发送一次 SendHeartBeat。 Parallel for SendHeartBeat 我需要调用 _testService.StartProcessAsync 从流中获取数据。它以某种方式停止工作并再次启动 LogOn 但我需要它直到它 returns false 它应该工作并且 SendHeartBeat 需要每 10 秒完成一次但不幸的是,它在 bool res = true 和这个的情况下停止工作此刻它不会抛出任何异常。有什么建议吗?

这是我根据 BackgroundService base class 中的 link 并“插入”您需要的功能:(当然未经测试)

public class TestBGService : BackGroundService
{
    // I guess you are using different logging
    private readonly ILogger<TestBGService> _logger; 
    // Your service to send Heartbeats to
    private readonly ITestService _testService;
    // If id is null, we need to login.
    private string _id = null;

    public TestBGService (ILogger<TestBGService> logger, ITestService testService)
    {
        _logger = logger;
        _testService = testService ?? throw new ArgumentNullException(nameof(testService));
    }

    public Task StartAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("TestBGService  running.");
        return Task.CompletedTask;
    }

    // This will be triggered by the runtime.
    private async Task ExecuteAsync (System.Threading.CancellationToken stoppingToken)
    {
        while( !stoppingToken.CancellationRequested )
        {
            try
            {
                if( string.IsNullOrEmpty(_id) )
                { 
                     // _id is null => perform login
                     _id = await _testService.LogOnAsync(); // perhaps pass stoppingToken?
                }
                bool res = await SendHeartBeat(id); // again: consider passing stoppingToken
                if( !res ) _id = null; // Heartbeat unsuccessful: login next time
                await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
            }
            catch(Exception ex) // You should actually catch more specific exceptions.
            {
                // TODO log Exception
                _id = null; // Reset id, so we login next time.
            }

        }
    }

    private async Task<bool> SendHeartBeat(string id)
    {
        // Don't even try if login was unsuccessful.
        if ( string.IsNullOrEmpty(id) ) return false;

        _logger.LogInformation("TestBGService sending heartbeat at {0}" , DateTime.Now);
        var response = await _testService.SendHeartBeatAsync(id);
        _logger.LogInformation("TestBGService heartbeat response {0}successful", response.IsSuccessful ? "" : "un");
        return response.IsSuccessful;
    }

    public Task StopAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("TestBGService  is stopping.");
        // TODO maybe explicit logout?
        return Task.CompletedTask;
    }
}