protected override async Task ExecuteAsync( CancellationToken cancellationToken) { _logger.LogInformation("Queued Hosted Service is starting."); while (!cancellationToken.IsCancellationRequested) { var(jobId, workItem) = await TaskQueue.DequeueAsync(cancellationToken); try { await workItem(cancellationToken); _jobStatusService.SetState(jobId, State.Finished); } catch (Exception ex) { _logger.LogError(ex, $"Error occurred executing {nameof(workItem)}."); _jobStatusService.SetState(jobId, State.Failed); } } _logger.LogInformation("Queued Hosted Service is stopping."); }