Table of Contents

Class RetryMiddleware

Namespace
WorkflowForge.Extensions.Resilience
Assembly
WorkflowForge.Extensions.Resilience.dll

Middleware that provides retry logic for workflow operations. Uses the configured resilience strategy to handle failures and retries.

public sealed class RetryMiddleware : IWorkflowOperationMiddleware
Inheritance
RetryMiddleware
Implements
Inherited Members

Constructors

RetryMiddleware(IWorkflowResilienceStrategy)

Initializes a new instance of the RetryMiddleware class.

public RetryMiddleware(IWorkflowResilienceStrategy retryStrategy)

Parameters

retryStrategy IWorkflowResilienceStrategy

The retry strategy to use.

Methods

Default(IWorkflowForgeLogger)

Creates retry middleware with default settings (3 attempts, 1 second fixed interval).

public static RetryMiddleware Default(IWorkflowForgeLogger logger)

Parameters

logger IWorkflowForgeLogger

The logger to use.

Returns

RetryMiddleware

A new retry middleware instance with default settings.

ExecuteAsync(IWorkflowOperation, IWorkflowFoundry, object?, Func<CancellationToken, Task<object?>>, CancellationToken)

Executes the operation middleware logic.

public Task<object?> ExecuteAsync(IWorkflowOperation operation, IWorkflowFoundry foundry, object? inputData, Func<CancellationToken, Task<object?>> next, CancellationToken cancellationToken = default)

Parameters

operation IWorkflowOperation

The operation being executed.

foundry IWorkflowFoundry

The foundry context providing properties, logger, and services.

inputData object

Input data passed to the operation.

next Func<CancellationToken, Task<object>>

Delegate to invoke the next middleware in the pipeline or the operation itself. Call this to continue the operation execution. Not calling this will short-circuit the operation.

cancellationToken CancellationToken

Cancellation token for the operation execution.

Returns

Task<object>

A task representing the operation execution result. The result can be null or any object depending on the operation.

Remarks

Middleware should:

  • Call next(cancellationToken) to continue operation execution
  • Handle exceptions appropriately (log, transform, or rethrow)
  • Clean up resources in finally blocks
  • Respect cancellation tokens
  • Preserve operation results unless intentionally transforming them

Example implementation:

public async Task<object?> ExecuteAsync(
    IWorkflowOperation operation,
    IWorkflowFoundry foundry,
    object? inputData,
    Func<CancellationToken, Task<object?>> next,
    CancellationToken cancellationToken)
{
    // Pre-execution logic
    Logger.LogInformation("Starting operation: {Name}", operation.Name);

    try
    {
        // Execute operation
        var result = await next(cancellationToken).ConfigureAwait(false);

        // Post-execution logic (success)
        Logger.LogInformation("Operation completed: {Name}", operation.Name);
        return result;
    }
    catch (Exception ex)
    {
        // Post-execution logic (failure)
        Logger.LogError(ex, "Operation failed: {Name}", operation.Name);
        throw;
    }
}

Exceptions

OperationCanceledException

Thrown when the operation execution is cancelled.

WithExponentialBackoff(IWorkflowForgeLogger, TimeSpan, TimeSpan, int, Func<Exception, bool>?)

Creates retry middleware with an exponential backoff strategy.

public static RetryMiddleware WithExponentialBackoff(IWorkflowForgeLogger logger, TimeSpan initialDelay, TimeSpan maxDelay, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null)

Parameters

logger IWorkflowForgeLogger

The logger to use.

initialDelay TimeSpan

The initial delay before the first retry.

maxDelay TimeSpan

The maximum delay between retries.

maxAttempts int

The maximum number of attempts (default: 3).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

Returns

RetryMiddleware

A new retry middleware instance.

WithFixedInterval(IWorkflowForgeLogger, TimeSpan, int, Func<Exception, bool>?)

Creates retry middleware with a fixed interval strategy.

public static RetryMiddleware WithFixedInterval(IWorkflowForgeLogger logger, TimeSpan interval, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null)

Parameters

logger IWorkflowForgeLogger

The logger to use.

interval TimeSpan

The fixed interval between retries.

maxAttempts int

The maximum number of attempts (default: 3).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

Returns

RetryMiddleware

A new retry middleware instance.

WithRandomInterval(IWorkflowForgeLogger, TimeSpan, TimeSpan, int, Func<Exception, bool>?)

Creates retry middleware with a random interval strategy.

public static RetryMiddleware WithRandomInterval(IWorkflowForgeLogger logger, TimeSpan minDelay, TimeSpan maxDelay, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null)

Parameters

logger IWorkflowForgeLogger

The logger to use.

minDelay TimeSpan

The minimum delay between retries.

maxDelay TimeSpan

The maximum delay between retries.

maxAttempts int

The maximum number of attempts (default: 3).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

Returns

RetryMiddleware

A new retry middleware instance.