RetrySyntax
Fluent API for defining a Retry Policy.
using Polly.Retry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
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 new RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry));
}, policyBuilder.ExceptionPredicates);
}
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 action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry, 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 new RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception, Context> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider)
{
Action<Exception, TimeSpan> 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 (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 action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy 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 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 action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, 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 (sleepDurations == null)
throw new ArgumentNullException("sleepDurations");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy 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 RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, 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, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetry));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, 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 action, Context context) {
RetryEngine.Implementation(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetry, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryAsync(this PolicyBuilder policyBuilder)
{
return policyBuilder.RetryAsync(1);
}
public static RetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount)
{
Action<Exception, int> onRetry = delegate {
};
return policyBuilder.RetryAsync(retryCount, onRetry);
}
public static RetryPolicy RetryAsync(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry)
{
return policyBuilder.RetryAsync(1, onRetry);
}
public static RetryPolicy RetryAsync(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 new RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryAsync(this PolicyBuilder policyBuilder, Action<Exception, int, Context> onRetry)
{
return policyBuilder.RetryAsync(1, onRetry);
}
public static RetryPolicy RetryAsync(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((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithCount(retryCount, onRetry, context), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder)
{
Action<Exception> onRetry = delegate {
};
return policyBuilder.RetryForeverAsync(onRetry);
}
public static RetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action<Exception> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action<Exception, Context> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry, context), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryAsync(retryCount, sleepDurationProvider, onRetry);
}
public static RetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> 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((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, 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((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryAsync(sleepDurations, onRetry);
}
public static RetryPolicy WaitAndRetryAsync(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 RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryAsync(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 RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryForeverAsync(sleepDurationProvider, onRetry);
}
public static RetryPolicy WaitAndRetryForeverAsync(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 new RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetry), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy((Func<CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => RetryEngine.ImplementationAsync(action, cancellationToken, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetry, context), continueOnCapturedContext), policyBuilder.ExceptionPredicates);
}
}
}