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

RetrySyntax

public static class RetrySyntax
Fluent API for defining a Retry Policy.
using Polly.Retry; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Polly { public static class RetrySyntax { public static Policy Retry(this PolicyBuilder policyBuilder) { return policyBuilder.Retry(1); } public static Policy Retry(this PolicyBuilder policyBuilder, int retryCount) { Action<Exception, int> onRetry = delegate { }; return policyBuilder.Retry(retryCount, onRetry); } public static Policy Retry(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry) { return policyBuilder.Retry(1, onRetry); } public static Policy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy(delegate(Action action) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry)); }, policyBuilder.ExceptionPredicates); } public static ContextualPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int, Context> onRetry) { return policyBuilder.Retry(1, onRetry); } public static ContextualPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int, Context> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new ContextualPolicy(delegate(Action action, Context context) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry, context)); }); } public static Policy RetryForever(this PolicyBuilder policyBuilder) { Action<Exception> onRetry = delegate { }; return policyBuilder.RetryForever(onRetry); } public static Policy RetryForever(this PolicyBuilder policyBuilder, Action<Exception> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy(delegate(Action action) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry)); }, policyBuilder.ExceptionPredicates); } public static ContextualPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception, Context> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return new ContextualPolicy(delegate(Action action, Context context) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry, context)); }); } public static Policy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider) { Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, onRetry); } public static Policy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider); return new Policy(delegate(Action action) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry)); }, policyBuilder.ExceptionPredicates); } public static ContextualPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider); return new ContextualPolicy(delegate(Action action, Context context) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context)); }); } public static Policy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations) { Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetry(sleepDurations, onRetry); } public static Policy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan> onRetry) { if (sleepDurations == null) throw new ArgumentNullException("sleepDurations"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy(delegate(Action action) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry)); }, policyBuilder.ExceptionPredicates); } public static ContextualPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, Context> onRetry) { if (sleepDurations == null) throw new ArgumentNullException("sleepDurations"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new ContextualPolicy(delegate(Action action, Context context) { RetryPolicy.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context)); }); } public static Policy RetryAsync(this PolicyBuilder policyBuilder) { return policyBuilder.RetryAsync(false); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, bool continueOnCapturedContext) { return policyBuilder.RetryAsync(1, continueOnCapturedContext); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount) { return policyBuilder.RetryAsync(retryCount, false); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, bool continueOnCapturedContext) { Action<Exception, int> onRetry = delegate { }; return policyBuilder.RetryAsync(retryCount, continueOnCapturedContext, onRetry); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry) { return policyBuilder.RetryAsync(false, onRetry); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, bool continueOnCapturedContext, Action<Exception, int> onRetry) { return policyBuilder.RetryAsync(1, continueOnCapturedContext, onRetry); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry) { return policyBuilder.RetryAsync(retryCount, false, onRetry); } public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, bool continueOnCapturedContext, Action<Exception, int> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates); } public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder) { return policyBuilder.RetryForeverAsync(false); } public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, bool continueOnCapturedContext) { Action<Exception> onRetry = delegate { }; return policyBuilder.RetryForeverAsync(continueOnCapturedContext, onRetry); } public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, Action<Exception> onRetry) { return policyBuilder.RetryForeverAsync(false, onRetry); } public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, bool continueOnCapturedContext, Action<Exception> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations) { return policyBuilder.WaitAndRetryAsync(sleepDurations, false); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, bool continueOnCapturedContext) { Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetryAsync(sleepDurations, continueOnCapturedContext, onRetry); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider) { return policyBuilder.WaitAndRetryAsync(retryCount, sleepDurationProvider, false); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, bool continueOnCapturedContext) { Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetryAsync(retryCount, sleepDurationProvider, continueOnCapturedContext, onRetry); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry) { return policyBuilder.WaitAndRetryAsync(retryCount, sleepDurationProvider, false, onRetry); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, bool continueOnCapturedContext, Action<Exception, TimeSpan> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero."); if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider); return new Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan> onRetry) { return policyBuilder.WaitAndRetryAsync(sleepDurations, false, onRetry); } public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, bool continueOnCapturedContext, Action<Exception, TimeSpan> onRetry) { if (sleepDurations == null) throw new ArgumentNullException("sleepDurations"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates); } } }