public async Task InvokeAsync(Context context, RequestProcessorCallback next) { int attempt = 0; do { try { await next(context); break; } catch (TaskCanceledException) { throw; } catch (OperationCanceledException) { throw; } catch (Exception e) { if (attempt++ >= _retryPolicy.RetryCount) { throw; } if (!_retryPolicy.RetryOnError(attempt, e)) { throw; } _logger.WillRetry(context, attempt, e); await Task.Delay(_retryPolicy.GetSleepDuration(attempt, e)); } } while (true); }