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

CircuitBreakerEngine

using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Polly.CircuitBreaker { internal class CircuitBreakerEngine { internal static void Implementation(Action action, Context context, IEnumerable<ExceptionPredicate> shouldHandlePredicates, ICircuitController breakerController) { breakerController.OnActionPreExecute(); try { action(); breakerController.OnActionSuccess(context); } catch (Exception ex2) { Exception ex; Exception ex3 = ex = ex2; if (!shouldHandlePredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; breakerController.OnActionFailure(ex, context); throw; } } internal static async Task ImplementationAsync(Func<CancellationToken, Task> action, Context context, IEnumerable<ExceptionPredicate> shouldHandlePredicates, ICircuitController breakerController, CancellationToken cancellationToken, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerController.OnActionSuccess(context); } catch (Exception ex2) { Exception ex = ex2; if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken) throw; cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandlePredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; breakerController.OnActionFailure(ex, context); throw; } } } }