RetryStrategyExtensions Class
- Namespace
- Kampute.Resilience
- Assembly
- Kampute.Resilience.dll
Definition
public static class RetryStrategyExtensions- Inheritance
- object
- RetryStrategyExtensions
Methods
Execute(this IRetryStrategy, Action<CancellationToken>, CancellationToken)
public static void Execute(this IRetryStrategy strategy, Action<CancellationToken> operation, CancellationToken cancellationToken = default)Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
operationAction<CancellationToken>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Exceptions
- ArgumentNullException
- Thrown if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Execute<T>(this IRetryStrategy, Func<CancellationToken, T>, CancellationToken)
public static T Execute<T>(this IRetryStrategy strategy, Func<CancellationToken, T> operation, CancellationToken cancellationToken = default)Type Parameters
T- The type of the value the operation returns.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
operationFunc<CancellationToken, T>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- T
- The value returned by the first successful run of the operation.
Exceptions
- ArgumentNullException
- Thrown if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
The calling thread is blocked during the waits between attempts; a wait ends as soon as cancellationToken is canceled. Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. To retry only selected exceptions, start a policy with RetryOn<TException>(this IRetryStrategy).
When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Execute<TState>(this IRetryStrategy, TState, Action<TState, CancellationToken>, CancellationToken)
public static void Execute<TState>(this
IRetryStrategy strategy,
TState state,
Action<TState, CancellationToken> operation,
CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
stateTState- The state passed to every attempt of the operation.
operationAction<TState, CancellationToken>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Exceptions
- ArgumentNullException
- Thrown if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
The calling thread is blocked during the waits between attempts. Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
Execute<TState, T>(this IRetryStrategy, TState, Func<TState, CancellationToken, T>, CancellationToken)
public static T Execute<TState, T>(this
IRetryStrategy strategy,
TState state,
Func<TState, CancellationToken, T> operation,
CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
T- The type of the value the operation returns.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
stateTState- The state passed to every attempt of the operation.
operationFunc<TState, CancellationToken, T>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- T
- The value returned by the first successful run of the operation.
Exceptions
- ArgumentNullException
- Thrown if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
The calling thread is blocked during the waits between attempts. Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
ExecuteAsync(this IRetryStrategy, Func<CancellationToken, Task>, CancellationToken)
public static Task ExecuteAsync(this IRetryStrategy strategy, Func<CancellationToken, Task> operation, CancellationToken cancellationToken = default)Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
operationFunc<CancellationToken, Task>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task
- A task that completes when the operation succeeds.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. To retry only selected exceptions, start a policy with RetryOn<TException>(this IRetryStrategy).
When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
ExecuteAsync<T>(this IRetryStrategy, Func<CancellationToken, Task<T>>, CancellationToken)
public static Task<T> ExecuteAsync<T>(this IRetryStrategy strategy, Func<CancellationToken, Task<T>> operation, CancellationToken cancellationToken = default)Type Parameters
T- The type of the value the operation returns.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
operationFunc<CancellationToken, Task<T>>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task<T>
- A task that resolves to the value returned by the first successful run of the operation.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. To retry only selected exceptions, start a policy with RetryOn<TException>(this IRetryStrategy).
When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
ExecuteAsync<TState>(this IRetryStrategy, TState, Func<TState, CancellationToken, Task>, CancellationToken)
public static Task ExecuteAsync<TState>(this
IRetryStrategy strategy,
TState state,
Func<TState, CancellationToken, Task> operation,
CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
stateTState- The state passed to every attempt of the operation.
operationFunc<TState, CancellationToken, Task>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task
- A task that completes when the operation succeeds.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
ExecuteAsync<TState, T>(this IRetryStrategy, TState, Func<TState, CancellationToken, Task<T>>, CancellationToken)
public static Task<T> ExecuteAsync<TState, T>(this
IRetryStrategy strategy,
TState state,
Func<TState, CancellationToken, Task<T>> operation,
CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
T- The type of the value the operation returns.
Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when to retry.
stateTState- The state passed to every attempt of the operation.
operationFunc<TState, CancellationToken, Task<T>>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task<T>
- A task that resolves to the value returned by the first successful run of the operation.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
strategyoroperationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Every exception is retried except an OperationCanceledException thrown after cancellationToken has been canceled. When the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
OnRetry(this IRetryStrategy, Action<RetryContext>)
public static RetryPolicy OnRetry(this IRetryStrategy strategy, Action<RetryContext> handler)Parameters
strategyIRetryStrategy- The strategy that decides whether and when to retry.
handlerAction<RetryContext>- The handler, which receives the failed attempt and the delay that execution waits before the retry.
Returns
- RetryPolicy
- A new RetryPolicy.
Exceptions
- ArgumentNullException
- Thrown if
strategyorhandlerisnull.
Remarks
OverrideDelay(this IRetryStrategy, Func<RetryContext, TimeSpan>)
public static RetryPolicy OverrideDelay(this IRetryStrategy strategy, Func<RetryContext, TimeSpan> delay)Parameters
strategyIRetryStrategy- The strategy that decides whether to retry and proposes the delay.
delayFunc<RetryContext, TimeSpan>- A function that receives the failed attempt, whose RetryContext.Delay is the delay that the strategy proposes, and returns the nonnegative delay to wait.
Returns
- RetryPolicy
- A new RetryPolicy.
Exceptions
- ArgumentNullException
- Thrown if
strategyordelayisnull.
Remarks
RetryOn(this IRetryStrategy, Func<Exception, bool>)
public static RetryPolicy RetryOn(this IRetryStrategy strategy, Func<Exception, bool> predicate)Parameters
strategyIRetryStrategy- The strategy that decides whether and when to retry.
predicateFunc<Exception, bool>- A function that returns
truefor the exceptions to retry.
Returns
- RetryPolicy
- A new RetryPolicy that retries only the exceptions that
predicateselects.
Exceptions
- ArgumentNullException
- Thrown if
strategyorpredicateisnull.
Remarks
RetryOn calls to retry other exceptions as well.RetryOn<TException>(this IRetryStrategy)
public static RetryPolicy RetryOn<TException>(this IRetryStrategy strategy)
where TException : ExceptionType Parameters
TException- The type of the exceptions to retry, including derived types.
Parameters
strategyIRetryStrategy- The strategy that decides whether and when to retry.
Returns
- RetryPolicy
- A new RetryPolicy that retries only the exceptions of type
TException.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull.
Remarks
RetryOn calls to retry other exceptions as well.RetryOn<TException>(this IRetryStrategy, Func<TException, bool>)
public static RetryPolicy RetryOn<TException>(this IRetryStrategy strategy, Func<TException, bool> predicate)
where TException : ExceptionType Parameters
TException- The type of the exceptions to retry, including derived types.
Parameters
strategyIRetryStrategy- The strategy that decides whether and when to retry.
predicateFunc<TException, bool>- A function that returns
truefor the exceptions of typeTExceptionto retry.
Returns
- RetryPolicy
- A new RetryPolicy that retries only the exceptions that
predicateselects.
Exceptions
- ArgumentNullException
- Thrown if
strategyorpredicateisnull.
Remarks
RetryOn calls to retry other exceptions as well.RetryOnResult<T>(this IRetryStrategy, Func<T, bool>)
T that retries every exception and the results that satisfy a condition, as the strategy decides.public static RetryPolicy<T> RetryOnResult<T>(this IRetryStrategy strategy, Func<T, bool> predicate)Type Parameters
T- The type returned by the operations the policy runs.
Parameters
strategyIRetryStrategy- The strategy that decides whether and when to retry.
predicateFunc<T, bool>- A function that returns
truefor the results to retry.
Returns
- RetryPolicy<T>
- A new RetryPolicy<T>.
Exceptions
- ArgumentNullException
- Thrown if
strategyorpredicateisnull.
Remarks
RetryOn calls to retry only selected exceptions.StartSession(this IRetryStrategy)
public static RetrySession StartSession(this IRetryStrategy strategy)Parameters
strategyIRetryStrategy- The retry strategy of the session.
Returns
- RetrySession
- A new RetrySession, with no retries and its elapsed time starting now.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull.
WithJitter(this IRetryStrategy, double)
public static JitterModifier WithJitter(this IRetryStrategy strategy, double jitterFactor = 0.5)Parameters
strategyIRetryStrategy- The retry strategy to modify.
jitterFactordouble optional- The largest proportion of each delay, between 0 and 1, by which the delay is randomly lengthened or shortened (optional). The default is 0.5.
Returns
- JitterModifier
- A JitterModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull. - ArgumentOutOfRangeException
- Thrown if
jitterFactoris not between 0 and 1.
WithMaxDelay(this IRetryStrategy, TimeSpan)
public static MaxDelayModifier WithMaxDelay(this IRetryStrategy strategy, TimeSpan maxDelay)Parameters
strategyIRetryStrategy- The retry strategy to modify.
maxDelayTimeSpan- The longest delay before a retry. Longer delays of
strategyare shortened to it.
Returns
- MaxDelayModifier
- A MaxDelayModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull. - ArgumentOutOfRangeException
- Thrown if
maxDelayis negative.
Remarks
maxDelay by the jitter factor; jitter added before the cap keeps every delay within maxDelay.WithMaxDelay(this IRetryStrategy, int)
public static MaxDelayModifier WithMaxDelay(this IRetryStrategy strategy, int millisecondsMaxDelay)Parameters
strategyIRetryStrategy- The retry strategy to modify.
millisecondsMaxDelayint- The longest delay before a retry, in milliseconds. Longer delays of
strategyare shortened to it.
Returns
- MaxDelayModifier
- A MaxDelayModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull. - ArgumentOutOfRangeException
- Thrown if
millisecondsMaxDelayis negative.
Remarks
WithMaxElapsedTime(this IRetryStrategy, TimeSpan)
public static MaxElapsedTimeModifier WithMaxElapsedTime(this IRetryStrategy strategy, TimeSpan maxElapsedTime)Parameters
strategyIRetryStrategy- The retry strategy to modify.
maxElapsedTimeTimeSpan- The time since the start of retry attempts after which no further retry is allowed. A delay that would end after it is shortened to end at it.
Returns
- MaxElapsedTimeModifier
- A MaxElapsedTimeModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull. - ArgumentOutOfRangeException
- Thrown if
maxElapsedTimeis negative.
Remarks
WithMaxElapsedTime(this IRetryStrategy, int)
public static MaxElapsedTimeModifier WithMaxElapsedTime(this IRetryStrategy strategy, int millisecondsMaxElapsedTime)Parameters
strategyIRetryStrategy- The retry strategy to modify.
millisecondsMaxElapsedTimeint- The number of milliseconds since the start of retry attempts after which no further retry is allowed. A delay that would end after it is shortened to end at it.
Returns
- MaxElapsedTimeModifier
- A MaxElapsedTimeModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull. - ArgumentOutOfRangeException
- Thrown if
millisecondsMaxElapsedTimeis negative.
Remarks
WithMaxRetries(this IRetryStrategy, uint)
public static MaxRetriesModifier WithMaxRetries(this IRetryStrategy strategy, uint maxRetries)Parameters
strategyIRetryStrategy- The retry strategy to modify.
maxRetriesuint- The maximum number of retries after the initial attempt. Zero allows no retry.
Returns
- MaxRetriesModifier
- A MaxRetriesModifier that wraps
strategy.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull.

The calling thread is blocked during the waits between attempts; a wait ends as soon as
cancellationTokenis canceled. Every exception is retried except an OperationCanceledException thrown aftercancellationTokenhas been canceled. To retry only selected exceptions, start a policy with RetryOn<TException>(this IRetryStrategy).When the strategy allows no more retries, the last exception is rethrown with its original stack trace.