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

RetryEngine

static class RetryEngine
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; namespace Polly.Retry { internal static class RetryEngine { public static void Implementation(Action action, IEnumerable<ExceptionPredicate> shouldRetryPredicates, Func<IRetryPolicyState> policyStateFactory) { IRetryPolicyState retryPolicyState = policyStateFactory(); Exception ex; while (true) { try { action(); return; } catch (Exception ex2) { Exception ex3 = ex = ex2; if (!shouldRetryPredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; if (!retryPolicyState.CanRetry(ex)) throw; } } } public static async Task ImplementationAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, IEnumerable<ExceptionPredicate> shouldRetryPredicates, Func<IRetryPolicyState> policyStateFactory, bool continueOnCapturedContext) { IRetryPolicyState policyState = policyStateFactory(); Exception ex; while (true) { cancellationToken.ThrowIfCancellationRequested(); try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); return; } catch (Exception ex2) { ex = ex2; if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken) { Exception obj = ex2 as Exception; if (obj == null) throw ex2; ExceptionDispatchInfo.Capture(obj).Throw(); } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldRetryPredicates.Any((ExceptionPredicate predicate) => predicate(ex))) { Exception obj2 = ex2 as Exception; if (obj2 == null) throw ex2; ExceptionDispatchInfo.Capture(obj2).Throw(); } if (!(await policyState.CanRetryAsync(ex, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext))) { Exception obj3 = ex2 as Exception; if (obj3 == null) throw ex2; ExceptionDispatchInfo.Capture(obj3).Throw(); } } } } } }