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

RetrySyntax

public static class RetrySyntax
Fluent API for defining a Retry Policy.
using Polly.Retry; using Polly.Utilities; using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace Polly { public static class RetrySyntax { public static RetryPolicy Retry(this PolicyBuilder policyBuilder) { return policyBuilder.Retry(1); } public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount) { Action<Exception, int> onRetry = delegate { }; return policyBuilder.Retry(retryCount, onRetry); } public static RetryPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry) { return policyBuilder.Retry(1, onRetry); } public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero."); if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.Retry(retryCount, delegate(Exception outcome, int i, Context ctx) { onRetry(outcome, i); }); } public static RetryPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int, Context> onRetry) { return policyBuilder.Retry(1, onRetry); } public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int, Context> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero."); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateRetryWithCount<EmptyStruct>(retryCount, delegate(DelegateResult<EmptyStruct> outcome, int i, Context ctx) { onRetry(outcome.Exception, i, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder) { Action<Exception> onRetry = delegate { }; return policyBuilder.RetryForever(onRetry); } public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.RetryForever(delegate(Exception outcome, Context ctx) { onRetry(outcome); }); } public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception, Context> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return new RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateRetryForever<EmptyStruct>(delegate(DelegateResult<EmptyStruct> outcome, Context ctx) { onRetry(outcome.Exception, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider) { Action<Exception, TimeSpan, int, Context> onRetry = delegate { }; return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, onRetry); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, delegate(Exception outcome, TimeSpan span, int i, Context ctx) { onRetry(outcome, span); }); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, delegate(Exception outcome, TimeSpan span, int i, Context ctx) { onRetry(outcome, span, ctx); }); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, int, Context> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to 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 RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateWaitAndRetry<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, int i, Context ctx) { onRetry(outcome.Exception, timespan, i, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, Context, TimeSpan> sleepDurationProvider) { Action<Exception, TimeSpan, int, Context> onRetry = delegate { }; return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, onRetry); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, Context, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, delegate(Exception outcome, TimeSpan span, int i, Context ctx) { onRetry(outcome, span, ctx); }); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, Context, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, int, Context> onRetry) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); return policyBuilder.WaitAndRetry(retryCount, (int i, Exception outcome, Context ctx) => sleepDurationProvider(i, ctx), onRetry); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, Exception, Context, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, int, Context> onRetry) { if (retryCount < 0) throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero."); if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateWaitAndRetryWithProvider<EmptyStruct>(retryCount, (int i, DelegateResult<EmptyStruct> outcome, Context ctx) => sleepDurationProvider(i, outcome.Exception, ctx), delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, int i, Context ctx) { onRetry(outcome.Exception, timespan, i, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations) { Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetry(sleepDurations, onRetry); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetry(sleepDurations, delegate(Exception outcome, TimeSpan span, int i, Context ctx) { onRetry(outcome, span); }); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, Context> onRetry) { if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetry(sleepDurations, delegate(Exception outcome, TimeSpan span, int i, Context ctx) { onRetry(outcome, span, ctx); }); } public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, int, Context> onRetry) { if (sleepDurations == null) throw new ArgumentNullException("sleepDurations"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateWaitAndRetry<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, int i, Context ctx) { onRetry(outcome.Exception, timespan, i, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); Action<Exception, TimeSpan> onRetry = delegate { }; return policyBuilder.WaitAndRetryForever(sleepDurationProvider, onRetry); } public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, Context, TimeSpan> sleepDurationProvider) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); Action<Exception, TimeSpan, Context> onRetry = delegate { }; return policyBuilder.WaitAndRetryForever(sleepDurationProvider, onRetry); } public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return policyBuilder.WaitAndRetryForever((int retryCount, Context context) => sleepDurationProvider(retryCount), delegate(Exception exception, TimeSpan timespan, Context context) { onRetry(exception, timespan); }); } public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, Context, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); return policyBuilder.WaitAndRetryForever((int i, Exception outcome, Context ctx) => sleepDurationProvider(i, ctx), onRetry); } public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, Exception, Context, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry) { if (sleepDurationProvider == null) throw new ArgumentNullException("sleepDurationProvider"); if (onRetry == null) throw new ArgumentNullException("onRetry"); return new RetryPolicy(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) { RetryEngine.Implementation(delegate(Context ctx, CancellationToken ct) { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryStateWaitAndRetryForever<EmptyStruct>((int i, DelegateResult<EmptyStruct> outcome, Context ctx) => sleepDurationProvider(i, outcome.Exception, ctx), delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) { onRetry(outcome.Exception, timespan, ctx); }, context)); }, policyBuilder.ExceptionPredicates); } } }