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

CircuitBreakerPolicy

using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Polly.CircuitBreaker { internal class CircuitBreakerPolicy { internal static void Implementation(Action action, IEnumerable<ExceptionPredicate> shouldRetryPredicates, ICircuitBreakerState breakerState) { if (breakerState.IsBroken) throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); try { action(); breakerState.Reset(); } catch (Exception ex2) { Exception ex; Exception ex3 = ex = ex2; if (!shouldRetryPredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; breakerState.TryBreak(ex); throw; } } internal static async Task ImplementationAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, IEnumerable<ExceptionPredicate> shouldHandlePredicates, ICircuitBreakerState breakerState, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); if (breakerState.IsBroken) throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerState.Reset(); } 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; breakerState.TryBreak(ex); throw; } } } }