Table of Contents

Class PollyResilienceStrategy

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

Enterprise-grade resilience strategy that uses Polly for advanced fault tolerance. Supports retry policies, circuit breakers and timeouts.

public sealed class PollyResilienceStrategy : IWorkflowResilienceStrategy
Inheritance
PollyResilienceStrategy
Implements
Inherited Members

Properties

Name

Gets the name of the resilience strategy.

public string Name { get; }

Property Value

string

Methods

CreateCircuitBreakerPolicy(int, TimeSpan?, int, IWorkflowForgeLogger?)

Creates a resilience strategy with circuit breaker policy.

public static PollyResilienceStrategy CreateCircuitBreakerPolicy(int failureThreshold = 5, TimeSpan? durationOfBreak = null, int minimumThroughput = 5, IWorkflowForgeLogger? logger = null)

Parameters

failureThreshold int

Number of failures before opening the circuit.

durationOfBreak TimeSpan?

Duration to keep the circuit open.

minimumThroughput int

Minimum number of actions in the time window.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

PollyResilienceStrategy

A new Polly resilience strategy.

CreateComprehensivePolicy(int, TimeSpan?, int, TimeSpan?, TimeSpan?, IWorkflowForgeLogger?)

Creates a comprehensive resilience strategy combining retry, circuit breaker, and timeout.

public static PollyResilienceStrategy CreateComprehensivePolicy(int maxRetryAttempts = 3, TimeSpan? baseDelay = null, int circuitBreakerThreshold = 5, TimeSpan? circuitBreakerDuration = null, TimeSpan? timeoutDuration = null, IWorkflowForgeLogger? logger = null)

Parameters

maxRetryAttempts int

Maximum number of retry attempts.

baseDelay TimeSpan?

Base delay for exponential backoff.

circuitBreakerThreshold int

Circuit breaker failure threshold.

circuitBreakerDuration TimeSpan?

Circuit breaker open duration.

timeoutDuration TimeSpan?

Operation timeout duration.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

PollyResilienceStrategy

A new comprehensive Polly resilience strategy.

CreateRetryPolicy(int, TimeSpan?, TimeSpan?, IWorkflowForgeLogger?)

Creates a resilience strategy with retry policy using exponential backoff.

public static PollyResilienceStrategy CreateRetryPolicy(int maxRetryAttempts = 3, TimeSpan? baseDelay = null, TimeSpan? maxDelay = null, IWorkflowForgeLogger? logger = null)

Parameters

maxRetryAttempts int

Maximum number of retry attempts.

baseDelay TimeSpan?

Base delay for exponential backoff.

maxDelay TimeSpan?

Maximum delay between retries.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

PollyResilienceStrategy

A new Polly resilience strategy.

CreateTimeoutPolicy(TimeSpan, IWorkflowForgeLogger?)

Creates a resilience strategy with timeout policy.

public static PollyResilienceStrategy CreateTimeoutPolicy(TimeSpan timeout, IWorkflowForgeLogger? logger = null)

Parameters

timeout TimeSpan

The timeout duration.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

PollyResilienceStrategy

A new Polly resilience strategy.

ExecuteAsync(Func<Task>, CancellationToken)

Executes an operation with resilience handling.

public 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 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.

GetRetryDelay(int, Exception?)

Gets the delay before the next retry attempt.

public 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 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.