RetrySession Class
- Namespace
- Kampute.Resilience
- Assembly
- Kampute.Resilience.dll
Definition
Represents the retry state of one operation that a retry strategy governs.
public class RetrySession : IRetrySession- Inheritance
- object
- RetrySession
- Implements
Remarks
Constructors
RetrySession(IRetryStrategy)
Initializes a new instance of the RetrySession class with a specified retry strategy.
public RetrySession(IRetryStrategy strategy)Parameters
strategyIRetryStrategy- The retry strategy that decides the delay before each retry.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull.
Properties
Elapsed
Gets the time elapsed since the session was created or last reset.
public virtual TimeSpan Elapsed { get; }Property Value
RetryCount
Gets the number of retries this session has allowed.
public virtual uint RetryCount { get; }Property Value
- uint
- The number of retries allowed since the session was created or last reset, not counting the initial attempt.
Strategy
Gets the retry strategy of this session.
public virtual IRetryStrategy Strategy { get; }Property Value
- IRetryStrategy
- The IRetryStrategy that decides the delay before each retry.
Methods
OnRetryScheduled()
Updates the state of the session when a retry is allowed.
protected virtual void OnRetryScheduled()Remarks
This method is called when TryGetRetryDelay(out TimeSpan) allows another retry with a delay that the session can wait, before the wait begins. The base implementation counts the retry.
Reset()
Resets the session to its initial state: no retries, and the elapsed time restarted.
public virtual void Reset()TryGetRetryDelay(out TimeSpan)
Decides whether another retry is allowed and, if so, the delay before it.
protected virtual bool TryGetRetryDelay(out TimeSpan delay)Parameters
Returns
Remarks
WaitToRetry(CancellationToken) and WaitToRetryAsync(CancellationToken) call this method once for each requested retry. The base implementation asks the Strategy, passing Elapsed and RetryCount. Override it to base the decision or the delay on more than the strategy; call the base implementation to keep the strategy's decision and limits.
The session applies the result: a negative delay is waited as zero, a delay longer than int.MaxValue milliseconds stops retrying, and an allowed retry is counted through OnRetryScheduled(). An override therefore does not count the retry itself.
WaitToRetry(CancellationToken)
Determines whether another retry is allowed and, if so, blocks the calling thread for the delay before it.
public virtual bool WaitToRetry(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- A token that can be used to cancel the wait.
Returns
Exceptions
- OperationCanceledException
- Thrown if
cancellationTokenis canceled. A wait in progress ends as soon as the token is canceled.
Remarks
TryGetRetryDelay(out TimeSpan) decides whether to retry and the delay; by default, the Strategy does.
WaitToRetryAsync(CancellationToken)
Determines whether another retry is allowed and, if so, asynchronously waits for the delay before it.
public virtual Task<bool> WaitToRetryAsync(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- A token that can be used to cancel the wait.
Returns
- Task<bool>
- A task that resolves to
trueif a retry should be attempted after the wait; otherwise,false.
Exceptions
- OperationCanceledException
- Thrown if
cancellationTokenis canceled.
Remarks
TryGetRetryDelay(out TimeSpan) decides whether to retry and the delay; by default, the Strategy does.

The session counts the retries and measures the time since it was created, and asks its Strategy for the delay before each retry. Use one session per operation; the strategy can be shared.
A derived class can take the decision or the delay from elsewhere, such as a retry time that the failure suggests or the state of the application, by overriding TryGetRetryDelay(out TimeSpan). The session still counts the retries and applies its delay limit.
The longest delay a session waits is int.MaxValue milliseconds (about 24.8 days), the limit of Task.Delay(TimeSpan, CancellationToken) on .NET Framework. If the strategy, or an override of TryGetRetryDelay(out TimeSpan), returns a longer delay, the session does not retry.