Net Core Worker 服务向主线程发送异常?

Net Core Worker Service sending an exception to main thread?

如果重试失败,我需要从 Main return 退出代码 -1,以便 (Kubernetes) 处理它,我尝试从 ExecuteAsync 抛出异常 (Task.FromException),但是方法签名会改变,并且不允许从 Task 更改为 Task<T>。我怎样才能向主线程提出 Environment.Exit(-1) ?或取消主线程,这样我就可以 return 从那里

退出代码 -1
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    int tries = 0;
    while (!stoppingToken.IsCancellationRequested && tries <= _tryOuts)
    {
        try
        {
                ...
                tries++;
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                ...
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    break;
                }
                else
                {
                    _logger.LogError(response.Message, DateTimeOffset.Now);
                }
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, ex.Message);
        }
        _logger.LogDebug("Worker next iteration in milliseconds {0}", _timeBetweenTryOuts);
        await Task.Delay(_timeBetweenTryOuts, stoppingToken);
    }
    _hostApplicationLifetime.StopApplication();
}

有趣的话题。我创建了 PoC,但在 Asp .NET Core 2.1(我现在没有版本 3)中,我能够 return 其他退出代码使用:

Environment.ExitCode

文档:https://docs.microsoft.com/en-us/dotnet/api/system.environment.exitcode?view=netcore-3.0

所以,尝试做类似的事情:

Environment.ExitCode = -1;
_hostApplicationLifetime.StopApplication();