ValidateMediatorHandlers() is a source-generated extension method on IServiceProvider that detects misconfigured handlers at startup — before the first request is processed. It resolves every registered handler, pipeline chain, and notification handler from DI, and throws an AggregateException containing all failures if any handler cannot be resolved.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMediator(b =>
{
b.AddOpenBehavior(typeof(LoggingBehavior<,>));
b.AddRequestPreProcessor<ValidationPreProcessor>();
});
var app = builder.Build();
// Fail fast — detect missing handlers, broken dependencies, incomplete pipelines
app.Services.ValidateMediatorHandlers();
app.Run();
This works with both registration approaches — AddMediator(configure) and the step-by-step RegisterMediatorHandlers() + PrecompilePipelines(). The validation method operates on the built IServiceProvider, not on the registration API.
| Handler type | Validation |
|---|---|
| Request handlers | GetRequiredService<IRequestHandler<T,R>>() + GetService<PipelineChainHandler<T,R>>() |
| Self-handling requests | GetRequiredService<IRequestHandler<T,R>>() (validates the generated adapter and its DI dependencies) |
| Notification handlers | GetServices<INotificationHandler<T>>() + materialization of all implementations |
| Stream handlers | GetRequiredService<IStreamRequestHandler<T,R>>() + GetService<StreamPipelineChainHandler<T,R>>() |
System.AggregateException: One or more mediator handlers failed validation.
---> System.InvalidOperationException: No service for type 'IRequestHandler<CreateUser, Guid>'
---> System.InvalidOperationException: Unable to resolve service for type 'IUserRepository'
while attempting to activate 'GetUserHandler'
Recommendation: Call
ValidateMediatorHandlers()in development and staging environments. In production, consider gating it behind a configuration flag to avoid the startup cost of resolving every handler.
The source generator emits ValidateMediatorHandlers() at compile time with explicit GetRequiredService<T>() calls for every handler it discovered. This means:
AggregateException, so you see every problem in one passAddMediator(configure) vs step-by-step registration