Table of Contents

Interface IWorkflowOperationMiddleware

Namespace
WorkflowForge.Abstractions
Assembly
WorkflowForge.dll

Middleware that executes at the operation level (wraps individual operation execution). Operation middleware executes INSIDE the foundry pipeline for each operation.

Execution Order:

Operation middleware wraps each operation execution in a Russian Doll pattern:

OperationMiddleware1 (outermost - first added)
  → OperationMiddleware2
    → OperationMiddleware3 (innermost - last added)
      → OPERATION EXECUTES
    ← OperationMiddleware3
  ← OperationMiddleware2
← OperationMiddleware1

Common Use Cases:

  • Error Handling: Catch and log operation exceptions
  • Timing: Measure operation execution time
  • Logging: Log operation start/completion with context
  • Validation: Validate operation inputs/outputs
  • Retry Logic: Retry failed operations with backoff
  • Caching: Cache operation results
  • Authorization: Check permissions before executing operation
public interface IWorkflowOperationMiddleware

Remarks

Unlike IWorkflowMiddleware which executes once per workflow, operation middleware executes for EACH operation in the workflow. This allows fine-grained control over individual operation behavior.

Methods

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

Executes the operation middleware logic.

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.