Class ResilienceStrategyBase
- Namespace
- WorkflowForge.Extensions.Resilience.Abstractions
- Assembly
- WorkflowForge.Extensions.Resilience.dll
Base class for resilience strategies that provides common retry logic including ShouldRetryAsync(int, Exception?, CancellationToken) and a shared retry loop.
public abstract class ResilienceStrategyBase : IWorkflowResilienceStrategy
- Inheritance
-
ResilienceStrategyBase
- Implements
- Derived
- Inherited Members
Constructors
ResilienceStrategyBase(string, int, Func<Exception, bool>?, IWorkflowForgeLogger?)
Initializes a new instance of the ResilienceStrategyBase class.
protected ResilienceStrategyBase(string name, int maxAttempts, Func<Exception, bool>? retryPredicate = null, IWorkflowForgeLogger? logger = null)
Parameters
namestringThe name of the strategy.
maxAttemptsintThe maximum number of attempts (including the initial attempt).
retryPredicateFunc<Exception, bool>Optional predicate to determine if an exception should be retried.
loggerIWorkflowForgeLoggerThe optional logger.
ResilienceStrategyBase(string, IWorkflowForgeLogger?)
Initializes a new instance of the ResilienceStrategyBase class without retry parameters (for strategies like Polly that handle retry internally).
protected ResilienceStrategyBase(string name, IWorkflowForgeLogger? logger = null)
Parameters
namestringThe name of the strategy.
loggerIWorkflowForgeLoggerThe optional logger.
Properties
Logger
Gets the logger for this strategy.
protected IWorkflowForgeLogger? Logger { get; }
Property Value
MaxAttempts
Gets the maximum number of attempts (including the initial attempt).
protected int MaxAttempts { get; }
Property Value
Name
Gets the name of the resilience strategy.
public string Name { get; }
Property Value
RetryPredicate
Gets the optional predicate to determine if a specific exception should be retried.
protected Func<Exception, bool>? RetryPredicate { get; }
Property Value
Methods
ExecuteAsync(Func<Task>, CancellationToken)
Executes an operation with resilience handling.
public abstract Task ExecuteAsync(Func<Task> operation, CancellationToken cancellationToken = default)
Parameters
operationFunc<Task>The operation to execute.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.
ExecuteAsync<T>(Func<Task<T>>, CancellationToken)
Executes an operation with resilience handling and returns a result.
public virtual Task<T> ExecuteAsync<T>(Func<Task<T>> operation, CancellationToken cancellationToken = default)
Parameters
operationFunc<Task<T>>The operation to execute.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<T>
A task representing the asynchronous operation with a result.
Type Parameters
TThe type of the result.
ExecuteWithRetryAsync(Func<Task>, CancellationToken)
Shared retry loop used by strategies that implement their own delay calculation but share the same attempt/retry/delay structure.
protected Task ExecuteWithRetryAsync(Func<Task> operation, CancellationToken cancellationToken)
Parameters
operationFunc<Task>cancellationTokenCancellationToken
Returns
GetRetryDelay(int, Exception?)
Gets the delay before the next retry attempt.
public abstract TimeSpan GetRetryDelay(int attemptNumber, Exception? exception)
Parameters
attemptNumberintThe current attempt number (1-based).
exceptionExceptionThe exception that occurred, if any.
Returns
- TimeSpan
The delay before the next retry attempt.
ShouldRetryAsync(int, Exception?, CancellationToken)
Determines whether a retry should be attempted based on the current attempt and exception.
public virtual Task<bool> ShouldRetryAsync(int attemptNumber, Exception? exception, CancellationToken cancellationToken = default)
Parameters
attemptNumberintThe current attempt number (1-based).
exceptionExceptionThe exception that occurred, if any.
cancellationTokenCancellationTokenThe cancellation token.