private async Task Process(IncomingMessage message, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested || message.HasLockExpired) { await messageSource .Abandon(message) .ConfigureAwait(false); return; } using var serviceScope = serviceScopeFactory.CreateScope(); var context = new Context( serviceScope, cancellationToken); try { await pipelineStartingAction(message, context) .ConfigureAwait(false); await messageSource .Complete(message) .ConfigureAwait(false); } catch (Exception exception) { // Behaviours should have logged/recorded/responded to this exception. if (message.DequeuedCount < retryOptions.MaximumImmediateAttempts) { await messageSource .Abandon(message) .ConfigureAwait(false); return; } if (message.DequeuedCount - retryOptions.MaximumImmediateAttempts < retryOptions.MaximumDeferredAttempts) { await messageSource .DeferUntil(message, DateTime.UtcNow + retryOptions.EffectiveDeferredRetryInterval, "Exception handling message", exception.ToString()) .ConfigureAwait(false); return; } await messageSource .DeadLetter(message, "Exception handling message", exception.ToString()) .ConfigureAwait(false); } }