Table of Contents

Class RetryWorkflowOperation

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

Wraps an operation with retry logic for transient failures using resilience strategies. Essential for building resilient workflows that handle and pass through data.

public sealed class RetryWorkflowOperation : WorkflowOperationBase, IWorkflowOperation, IDisposable
Inheritance
RetryWorkflowOperation
Implements
Inherited Members
Extension Methods

Constructors

RetryWorkflowOperation(IWorkflowOperation, IWorkflowResilienceStrategy, string?, Guid?)

Initializes a new instance of the RetryWorkflowOperation class.

public RetryWorkflowOperation(IWorkflowOperation operation, IWorkflowResilienceStrategy retryStrategy, string? name = null, Guid? id = null)

Parameters

operation IWorkflowOperation

The operation to retry.

retryStrategy IWorkflowResilienceStrategy

The resilience strategy to use for retry logic.

name string

Optional name for the retry operation.

id Guid?

Optional operation ID.

Properties

Id

Gets the unique identifier for this operation.

public override Guid Id { get; }

Property Value

Guid

Name

Gets the name of this operation for identification and logging purposes.

public override string Name { get; }

Property Value

string

Methods

Create(IWorkflowOperation, int, string?, IWorkflowForgeLogger?)

Creates a retry operation with default exponential backoff settings.

public static RetryWorkflowOperation Create(IWorkflowOperation operation, int maxAttempts = 3, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

maxAttempts int

Maximum number of attempts (default: 3).

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.

Dispose(bool)

Releases managed resources. Override in derived classes to dispose of owned resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

True if called from Dispose(), false if from a finalizer.

ForTransientErrors(IWorkflowOperation, int, string?, IWorkflowForgeLogger?)

Creates a retry operation optimized for transient errors.

public static RetryWorkflowOperation ForTransientErrors(IWorkflowOperation operation, int maxAttempts = 3, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

maxAttempts int

Maximum number of attempts (default: 3).

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.

ForgeAsyncCore(object?, IWorkflowFoundry, CancellationToken)

Core operation logic. Implement this method with your operation's business logic. This is called between OnBeforeExecuteAsync and OnAfterExecuteAsync.

protected override Task<object?> ForgeAsyncCore(object? inputData, IWorkflowFoundry foundry, CancellationToken cancellationToken = default)

Parameters

inputData object

The input data for the operation.

foundry IWorkflowFoundry

The workflow foundry providing context and services.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<object>

The output data from the operation.

FromSettings(IWorkflowOperation, RetryPolicySettings, Func<Exception, bool>?, string?, IWorkflowForgeLogger?)

Creates a retry operation from retry policy settings.

public static RetryWorkflowOperation FromSettings(IWorkflowOperation operation, RetryPolicySettings settings, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

settings RetryPolicySettings

The retry policy settings.

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.

RestoreAsync(object?, IWorkflowFoundry, CancellationToken)

Restores (compensates) the operation's effects to undo its changes. Used for rollback scenarios when subsequent operations fail.

public override Task RestoreAsync(object? outputData, IWorkflowFoundry foundry, CancellationToken cancellationToken = default)

Parameters

outputData object

The data that was produced by this operation during ForgeAsync.

foundry IWorkflowFoundry

The workflow foundry providing execution context and shared services.

cancellationToken CancellationToken

The cancellation token for cooperative cancellation.

Returns

Task

A task representing the asynchronous restoration.

Remarks

The base implementation is a no-op. Override this method in your operation to provide compensation/rollback logic. Operations that don't override this are safely skipped during compensation.

Exceptions

ArgumentNullException

Thrown when foundry is null.

ObjectDisposedException

Thrown when the operation has been disposed.

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

Creates a retry operation with exponential backoff strategy.

public static RetryWorkflowOperation WithExponentialBackoff(IWorkflowOperation operation, TimeSpan? baseDelay = null, TimeSpan? maxDelay = null, int maxAttempts = 3, double backoffMultiplier = 2, Func<Exception, bool>? retryPredicate = null, bool enableJitter = true, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

baseDelay TimeSpan?

Base delay for exponential backoff.

maxDelay TimeSpan?

Maximum delay between retries.

maxAttempts int

Maximum number of attempts (default: 3).

backoffMultiplier double

Multiplier for exponential backoff (default: 2.0).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

enableJitter bool

Whether to enable jitter in retry delays (default: true).

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.

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

Creates a retry operation with a fixed interval strategy.

public static RetryWorkflowOperation WithFixedInterval(IWorkflowOperation operation, TimeSpan interval, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

interval TimeSpan

The fixed interval between retries.

maxAttempts int

Maximum number of attempts (default: 3).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.

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

Creates a retry operation with random interval strategy.

public static RetryWorkflowOperation WithRandomInterval(IWorkflowOperation operation, TimeSpan minDelay, TimeSpan maxDelay, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)

Parameters

operation IWorkflowOperation

The operation to retry.

minDelay TimeSpan

Minimum delay between retries.

maxDelay TimeSpan

Maximum delay between retries.

maxAttempts int

Maximum number of attempts (default: 3).

retryPredicate Func<Exception, bool>

Optional predicate to determine which exceptions to retry.

name string

Optional name for the retry operation.

logger IWorkflowForgeLogger

Optional logger for retry events.

Returns

RetryWorkflowOperation

A new retry operation instance.