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

CircuitBreakerPolicy

using Polly.Extensions; using System; using System.Collections.Generic; using System.Linq; 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 = ex2; if (!shouldRetryPredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; breakerState.TryBreak(ex); throw; } } internal static async Task ImplementationAsync(Func<Task> 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 { await action().NotOnCapturedContext(); breakerState.Reset(); } catch (Exception ex2) { Exception ex = ex2; if (!shouldRetryPredicates.Any((ExceptionPredicate predicate) => predicate(ex))) throw; breakerState.TryBreak(ex); throw; } } } }