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 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));
});
}
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 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));
});
}
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 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));
});
}
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 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));
});
}
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(1);
}
public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount)
{
Action<Exception, int> onRetry = delegate {
};
return policyBuilder.RetryAsync(retryCount, onRetry);
}
public static Policy RetryAsync(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry)
{
return policyBuilder.RetryAsync(1, onRetry);
}
public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry)
{
if (retryCount <= 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be 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)));
}
public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder)
{
Action<Exception> onRetry = delegate {
};
return policyBuilder.RetryForeverAsync(onRetry);
}
public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, Action<Exception> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyState(onRetry)));
}
public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryAsync(sleepDurations, onRetry);
}
public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryAsync(retryCount, sleepDurationProvider, onRetry);
}
public static Policy 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 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)));
}
public static Policy 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 Policy((Func<Task> action) => RetryPolicy.ImplementationAsync(action, policyBuilder.ExceptionPredicates, () => new RetryPolicyStateWithSleep(sleepDurations, onRetry)));
}
}
}