Table of Contents

Interface IWorkflowMiddleware

Namespace
WorkflowForge.Abstractions
Assembly
WorkflowForge.dll

Middleware that executes at the workflow level (wraps entire workflow execution). Workflow middleware executes OUTSIDE the foundry pipeline and can affect the entire workflow.

Execution Order:

Workflow middleware wraps the entire workflow execution, including all operations and operation-level middleware:

WorkflowMiddleware1 (outermost)
  → WorkflowMiddleware2
    → WORKFLOW START
      → OperationMiddleware1 (per operation)
        → OperationMiddleware2
          → OPERATION EXECUTES
      → Next operation...
    ← WORKFLOW END
  ← WorkflowMiddleware2
← WorkflowMiddleware1

Common Use Cases:

  • Workflow Timeouts: Enforce maximum execution time for entire workflow
  • Workflow Logging: Log workflow start/completion with consolidated metrics
  • Workflow Authorization: Check permissions before allowing workflow execution
  • Workflow Caching: Cache workflow results based on inputs
  • Workflow Metrics: Collect aggregate metrics across all operations
public interface IWorkflowMiddleware

Remarks

Unlike IWorkflowOperationMiddleware which executes per operation, workflow middleware executes once per workflow and can short-circuit the entire workflow execution. This is useful for cross-cutting concerns that apply to the workflow as a whole.

Methods

ExecuteAsync(IWorkflow, IWorkflowFoundry, Func<Task>, CancellationToken)

Executes the workflow middleware logic.

Task ExecuteAsync(IWorkflow workflow, IWorkflowFoundry foundry, Func<Task> next, CancellationToken cancellationToken = default)

Parameters

workflow IWorkflow

The workflow being executed.

foundry IWorkflowFoundry

The foundry context for the workflow execution.

next Func<Task>

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

cancellationToken CancellationToken

Cancellation token for the workflow execution.

Returns

Task

A task representing the workflow execution.

Remarks

Middleware should:

  • Call next() to continue workflow execution
  • Handle exceptions appropriately (log, transform, or rethrow)
  • Clean up resources in finally blocks
  • Respect cancellation tokens

Example implementation:

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

    try
    {
        // Execute workflow
        await next().ConfigureAwait(false);

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

Exceptions

OperationCanceledException

Thrown when the workflow execution is cancelled.