<PackageReference Include="Polly" Version="8.4.0" />

AsyncRetryPolicy

A retry policy that can be applied to asynchronous delegates.
using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace Polly.Retry { public class AsyncRetryPolicy : AsyncPolicy, IRetryPolicy, IsPolicy { private readonly Func<Exception, TimeSpan, int, Context, Task> _onRetryAsync; private readonly int _permittedRetryCount; private readonly IEnumerable<TimeSpan> _sleepDurationsEnumerable; private readonly Func<int, Exception, Context, TimeSpan> _sleepDurationProvider; internal AsyncRetryPolicy(PolicyBuilder policyBuilder, Func<Exception, TimeSpan, int, Context, Task> onRetryAsync, int permittedRetryCount = int.MaxValue, IEnumerable<TimeSpan> sleepDurationsEnumerable = null, Func<int, Exception, Context, TimeSpan> sleepDurationProvider = null) : base(policyBuilder) { _permittedRetryCount = permittedRetryCount; _sleepDurationsEnumerable = sleepDurationsEnumerable; _sleepDurationProvider = sleepDurationProvider; if (onRetryAsync == null) throw new ArgumentNullException("onRetryAsync"); _onRetryAsync = onRetryAsync; } [DebuggerStepThrough] protected override Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { Func<int, DelegateResult<TResult>, Context, TimeSpan> sleepDurationProvider = (_sleepDurationProvider != null) ? ((Func<int, DelegateResult<TResult>, Context, TimeSpan>)((int retryCount, DelegateResult<TResult> outcome, Context ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx))) : null; return AsyncRetryEngine.ImplementationAsync(action, context, cancellationToken, base.ExceptionPredicates, ResultPredicates<TResult>.None, (DelegateResult<TResult> outcome, TimeSpan timespan, int retryCount, Context ctx) => _onRetryAsync(outcome.Exception, timespan, retryCount, ctx), _permittedRetryCount, _sleepDurationsEnumerable, sleepDurationProvider, continueOnCapturedContext); } } }