Net Core Worker Service sending an exception to main thread?

Viewed 1900

I need to return Exit Code -1 from Main if retries failed so (Kubernetes) handle it, I try throwing an exception (Task.FromException) from ExecuteAsync but the method signature will change and it is not allowed to change from Task to Task<T>. How can I raised a Environment.Exit(-1) to the main thread? or cancellation to main thread so I can return an exit code -1 from there

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