Table of Contents

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

name string

The name of the strategy.

maxAttempts int

The maximum number of attempts (including the initial attempt).

retryPredicate Func<Exception, bool>

Optional predicate to determine if an exception should be retried.

logger IWorkflowForgeLogger

The 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

name string

The name of the strategy.

logger IWorkflowForgeLogger

The optional logger.

Properties

Logger

Gets the logger for this strategy.

protected IWorkflowForgeLogger? Logger { get; }

Property Value

IWorkflowForgeLogger

MaxAttempts

Gets the maximum number of attempts (including the initial attempt).

protected int MaxAttempts { get; }

Property Value

int

Name

Gets the name of the resilience strategy.

public string Name { get; }

Property Value

string

RetryPredicate

Gets the optional predicate to determine if a specific exception should be retried.

protected Func<Exception, bool>? RetryPredicate { get; }

Property Value

Func<Exception, bool>

Methods

ExecuteAsync(Func<Task>, CancellationToken)

Executes an operation with resilience handling.

public abstract Task ExecuteAsync(Func<Task> operation, CancellationToken cancellationToken = default)

Parameters

operation Func<Task>

The operation to execute.

cancellationToken CancellationToken

The 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

operation Func<Task<T>>

The operation to execute.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<T>

A task representing the asynchronous operation with a result.

Type Parameters

T

The 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

operation Func<Task>
cancellationToken CancellationToken

Returns

Task

GetRetryDelay(int, Exception?)

Gets the delay before the next retry attempt.

public abstract TimeSpan GetRetryDelay(int attemptNumber, Exception? exception)

Parameters

attemptNumber int

The current attempt number (1-based).

exception Exception

The 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

attemptNumber int

The current attempt number (1-based).

exception Exception

The exception that occurred, if any.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<bool>

True if a retry should be attempted; otherwise, false.