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.");
        }
예제 #2
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            try
            {
                await AddRequestAuditRecord(context);
            }
            catch (Exception exception)
            {
                _errorReportingService.CaptureException(exception);
            }

            await next();
        }
예제 #3
0
        private async Task RefreshToken()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                if (!(scope.ServiceProvider.GetRequiredService <IMicroServiceTokenProvider>() is CachedMicroServiceTokenProvider cachedMicroServiceTokenProvider))
                {
                    _errorReportingService.CaptureException(new Exception("Critical error: Expected IMicroServiceTokenProvider to be of type CachedMicroServiceTokenProvider in TokenRefresherService"));

                    return;
                }

                await cachedMicroServiceTokenProvider.GetToken();
            }
        }
예제 #4
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                _errorReportingService.CaptureException(exception);

                if (context.Response.HasStarted)
                {
                    _logger.LogWarning("The response has already started, the FriendlyExceptionsMiddleware will not be executed.");
                    throw;
                }

                await context.HandleExceptionAsync(_options, exception);
            }
        }