protected override async Task ExecuteAsync(CancellationToken cancellationToken) { _logger.LogDebug($"{nameof(RecurringBackgroundService)} is starting."); cancellationToken.Register(() => _logger.LogWarning($"{nameof(RecurringBackgroundService)} background task is stopping.")); int iterationNumber = 0; while (!cancellationToken.IsCancellationRequested) { _logger.LogDebug($"{nameof(RecurringBackgroundService)} task doing background work."); try { iterationNumber++; await ExecuteIteration(cancellationToken, iterationNumber); } catch (Exception exception) { _errorReportingService.CaptureException(exception); var canContinue = CanContinueAfterException(exception); if (!canContinue) { _errorReportingService.CaptureWarning($"{nameof(CanContinueAfterException)} is FALSE so will now stop execution of {nameof(RecurringBackgroundService)}"); return; } } await Task.Delay(LoopInterval, cancellationToken); } _logger.LogDebug($"{nameof(RecurringBackgroundService)} background task is stopping."); }