Class TypedExceptionHandlerMiddleware
- Namespace
- Excalibur.Dispatch.Middleware.ErrorHandling
- Assembly
- Excalibur.Dispatch.dll
Middleware that routes exceptions to typed exception handlers registered in the DI container.
[AppliesTo(MessageKinds.All)]
public sealed class TypedExceptionHandlerMiddleware : IDispatchMiddleware
- Inheritance
-
TypedExceptionHandlerMiddleware
- Implements
- Inherited Members
- Extension Methods
Remarks
This middleware catches exceptions from downstream pipeline components and attempts to route them to a registered ITypedExceptionHandler<TException> for the exception's type. If no handler is found for the exact type, the exception type hierarchy is walked upward. If no handler handles the exception, it is re-thrown.
OperationCanceledException is never routed to handlers and always propagates.
Constructors
TypedExceptionHandlerMiddleware(IServiceProvider, ILogger<TypedExceptionHandlerMiddleware>)
Middleware that routes exceptions to typed exception handlers registered in the DI container.
public TypedExceptionHandlerMiddleware(IServiceProvider serviceProvider, ILogger<TypedExceptionHandlerMiddleware> logger)
Parameters
serviceProviderIServiceProviderThe service provider for resolving typed handlers.
loggerILogger<TypedExceptionHandlerMiddleware>The logger for diagnostic output.
Remarks
This middleware catches exceptions from downstream pipeline components and attempts to route them to a registered ITypedExceptionHandler<TException> for the exception's type. If no handler is found for the exact type, the exception type hierarchy is walked upward. If no handler handles the exception, it is re-thrown.
OperationCanceledException is never routed to handlers and always propagates.
Properties
Stage
Gets the pipeline stage where this middleware should execute.
public DispatchMiddlewareStage? Stage { get; }
Property Value
- DispatchMiddlewareStage?
The pipeline stage for this middleware.
Remarks
The stage determines the execution order of middleware in the pipeline. If null, the middleware will be registered at the end of the pipeline. Stages are processed in numeric order, allowing fine-grained control over middleware execution sequence.
Methods
InvokeAsync(IDispatchMessage, IMessageContext, DispatchRequestDelegate, CancellationToken)
Processes the message within the middleware pipeline.
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "MakeGenericType is used to resolve typed exception handlers. Register handlers explicitly for AOT scenarios.")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "Handler types are preserved through DI registration. Reflection is used for dynamic dispatch to typed handlers.")]
public ValueTask<IMessageResult> InvokeAsync(IDispatchMessage message, IMessageContext context, DispatchRequestDelegate nextDelegate, CancellationToken cancellationToken)
Parameters
messageIDispatchMessageThe message being processed.
contextIMessageContextThe message context containing metadata and processing state.
nextDelegateDispatchRequestDelegateThe delegate to invoke the next middleware in the pipeline.
cancellationTokenCancellationTokenThe cancellation token to observe.
Returns
- ValueTask<IMessageResult>
A ValueTask<TResult> representing the result of message processing, potentially modified by this middleware. ValueTask can avoid allocating a Task for a synchronously completed result; other dispatch work can still allocate.
Remarks
Implementations can execute logic before calling nextDelegate, after calling it, or both. To short-circuit the pipeline, don't call nextDelegate and return an appropriate result. The context can be used to share data between middleware components. Respect the cancellation token to support timeouts and graceful shutdown.
Exceptions
- ArgumentNullException
Thrown when message, context, or nextDelegate is null.
- OperationCanceledException
Thrown when the operation is cancelled.