CatalogMediator
Handler Pipeline
- ID:
mediator.handler-pipeline - Name: Handler pipeline
- Maturity: GA
- Summary: Defines pre, main, post, and error stage execution for command, query, and event mediation.
What It Does
The pipeline is shared across semantic mediators:
- Pre-handlers.
- Main handler(s).
- Post-handlers.
- Error-handlers on recoverable failures.
Direct and indirect handlers run in a fixed order:
- Pre: indirect then direct.
- Post: direct then indirect.
- Error: indirect then direct.
Single-handler strategies (commands and queries) support Abort(...). Broadcast strategy (events) uses its own fan-out flow and does not use abort as a success path.
Public Surface
public sealed class AuditPreHandler : ICommandPreHandler<CreateOrderCommand>
{
public Task PreHandleAsync(CreateOrderCommand message, CancellationToken cancellationToken = default)
{
AmbientExecutionContext.Current.Items["trace"] = "pre-stage";
return Task.CompletedTask;
}
}| API | Role |
|---|---|
IAsyncMessagePreHandler<TMessage> | Generic pre stage contract |
IAsyncMessageHandler<TMessage> / IAsyncMessageHandler<TMessage, TResult> | Main handler contracts |
IAsyncMessagePostHandler<TMessage> / IAsyncMessagePostHandler<TMessage, TResult> | Post stage contracts |
IAsyncMessageErrorHandler<TMessage, TResult> | Error stage contract |
MessageErrorContext<TMessage, TResult> | Typed error data and shared recovery outcome |
SingleAsyncHandlerMediationStrategy<TMessage, TResult> | Single main handler orchestration |
SingleStreamHandlerMediationStrategy<TMessage, TResult> | Stream query orchestration |
AsyncBroadcastMediationStrategy<TMessage> | Event broadcast orchestration |
MessageContextExtensions.RunAsyncPreHandlers/RunAsyncPostHandlers/RunAsyncErrorHandlers | Stage execution helpers |
Packages
LiteBus.MessagingLiteBus.Messaging.Abstractions
Requires
mediator.execution-contextmediator.handler-prioritymediator.handler-filtering
Invariants
- Single-handler strategies resolve exactly one main handler.
- Post-handler result override uses
executionContext.MessageResult. - Recoverable exceptions route to error handlers with the caller's explicit cancellation token.
- Error handlers suppress the original exception only by setting their shared context outcome to
Handled. - Event broadcast may execute handlers concurrently based on event execution settings.
Non-Goals
- Automatic retries or delayed re-execution on failures.
- Transaction boundaries across multiple handlers.
- Pipeline-level persistence of stage artifacts.
Observability
No pipeline-specific meter, activity source, or structured event catalog is exposed in mediator packages.
Operational alternatives:
- Add application logs in pre/post/error handlers.
- Add custom timing in handlers using
AmbientExecutionContext.Items.
Test Coverage
Covered
| Test method | Project |
|---|---|
Send_CreateProductCommand_ShouldGoThroughHandlersCorrectly | LiteBus.Mediator.UnitTests |
Mediating_GetProductQuery_ShouldGoThroughHandlersCorrectly | LiteBus.Mediator.UnitTests |
mediating_event_with_exception_in_main_handler_goes_through_error_handlers | LiteBus.Mediator.UnitTests |
mediating_a_command_that_is_aborted_in_pre_handler_goes_through_correct_handlers | LiteBus.Mediator.UnitTests |
Send_CommandWithResult_PostHandlerOverridesResult | LiteBus.Mediator.UnitTests |
Send_Command_WithErrorHandler_ShouldPassTypedContextAndExplicitCancellationToken | LiteBus.Mediator.UnitTests |
Send_Command_WithObservingErrorHandler_ShouldRethrowByDefault | LiteBus.Mediator.UnitTests |
Send_CommandWithResult_WhenErrorHandlerSetsHandledResult_ShouldReturnFallbackResult | LiteBus.Mediator.UnitTests |
Untested
- Deep nested indirect handler chains mixed with large tag filters and predicate filters.
- Pipeline memory behavior for extremely long stream lifetimes.
Out-of-Scope
- Broker transport ingress/dispatch pipelines.
- Durable inbox/outbox processor pipeline behaviors.