Class CircuitBreakerMiddleware
- Namespace
- WorkflowForge.Extensions.Resilience
- Assembly
- WorkflowForge.Extensions.Resilience.dll
Circuit breaker middleware that prevents cascading failures by temporarily blocking operations when failure rates exceed configured thresholds.
public sealed class CircuitBreakerMiddleware : IWorkflowOperationMiddleware, IDisposable
- Inheritance
-
CircuitBreakerMiddleware
- Implements
- Inherited Members
Constructors
CircuitBreakerMiddleware(ICircuitBreakerPolicy, IWorkflowForgeLogger?, string?)
Initializes a new instance of the CircuitBreakerMiddleware class.
public CircuitBreakerMiddleware(ICircuitBreakerPolicy policy, IWorkflowForgeLogger? logger = null, string? name = null)
Parameters
policyICircuitBreakerPolicyThe circuit breaker policy to use.
loggerIWorkflowForgeLoggerOptional logger for circuit breaker events.
namestringOptional name for the middleware.
Properties
Name
Gets the name of the middleware for identification purposes.
public string Name { get; }
Property Value
Methods
Dispose()
Disposes the policy supplied to the constructor.
public void Dispose()
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
operationIWorkflowOperationThe operation being executed.
foundryIWorkflowFoundryThe foundry context providing properties, logger, and services.
inputDataobjectInput data passed to the operation.
nextFunc<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.
cancellationTokenCancellationTokenCancellation 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.