MediatR Send fails because DI has no handler for the request
Your error
System.InvalidOperationException: No service for type 'MediatR.IRequestHandler`2[MyShop.Application.Orders.CreateOrderCommand,System.Int32]' has been registered.1 more line it can showFix it fast
Most common cause, 1 of 3AddMediatR scans the web project, but the handlers live in another project
Point the scan at the assembly that holds the handlers.
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<CreateOrderHandler>();
// or several: cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly, typeof(CreateOrderHandler).Assembly);
});Short answer
InvalidOperationException: No service for type 'MediatR.IRequestHandler`2[...]' has been registered means Send() asked dependency injection for a handler that AddMediatR never registered. Usually the handlers sit in another project than the assembly you scanned, the handler's generic types differ from the request, or the handler is an open generic that needs RegisterGenericHandlers. Applies to MediatR 12 to 15.
Which cause do you have?
List every request handler that ended up in the service collection right after AddMediatR. Compare the printed service types with the type inside the error message: a missing assembly, a type mismatch and an unregistered generic handler each show up differently.
// Program.cs, temporarily, after builder.Services.AddMediatR(...)
var handlerDefs = new[] { typeof(IRequestHandler<,>), typeof(IRequestHandler<>) };
foreach (var d in builder.Services.Where(s => s.ServiceType.IsGenericType
&& handlerDefs.Contains(s.ServiceType.GetGenericTypeDefinition())))
Console.WriteLine($"{d.ServiceType} -> {d.ImplementationType}");Then find the line you see in this list and open its fix:
- 1AddMediatR scans the web project, but the handlers live in another project
The printed list is empty or holds only handlers from the API project, while CreateOrderHandler sits in an Application or Core class library - 2The handler's generic arguments do not match the request
The list shows a handler for the request, but with a different response type, or for a class with the same name in another namespace - 3The handler is an open generic class and generic registration is off
The handler is declared like GetByIdHandler<T> : IRequestHandler<GetById<T>, T>, and the error names a closed type such as GetById`1[Customer]
What does the error look like?
System.InvalidOperationException: No service for type 'MediatR.IRequestHandler`2[MyShop.Application.Orders.CreateOrderCommand,System.Int32]' has been registered.No service for type 'MediatR.IRequestHandler`1[MyShop.Application.Orders.ArchiveOrderCommand]' has been registered.
Cause 1 of 3
AddMediatR scans the web project, but the handlers live in another project
You seeThe printed list is empty or holds only handlers from the API project, while CreateOrderHandler sits in an Application or Core class library
Check
This No service for type error appears when MediatR never saw the handler class. AddMediatR registers handlers only from the assemblies you name, so RegisterServicesFromAssemblyContaining<Program>() covers the web project alone and skips handlers in a separate class library.
grep -rn "RegisterServicesFrom" --include=*.cs .Fix
Point the scan at the assembly that holds the handlers. Name a type from that project, and list several assemblies when handlers are spread across projects.
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<CreateOrderHandler>();
// or several: cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly, typeof(CreateOrderHandler).Assembly);
});Verify
The first-step loop now prints IRequestHandler`2[...CreateOrderCommand,System.Int32] next to CreateOrderHandler, and Send() returns a result.
dotnet run --project src/MyShop.ApiConfirmed by raw.githubusercontent.com
Cause 2 of 3
The handler's generic arguments do not match the request
You seeThe list shows a handler for the request, but with a different response type, or for a class with the same name in another namespace
Check
MediatR resolves the exact closed type IRequestHandler<TRequest, TResponse> from the request it was given. A command declared as IRequest<int> is not served by a handler for IRequestHandler<CreateOrderCommand, long>, and two CreateOrderCommand classes in different namespaces are two different types.
grep -rnE "class CreateOrderCommand|IRequestHandler<CreateOrderCommand" --include=*.cs .Fix
Make the handler implement IRequestHandler with the same request class and the same TResponse that the request declares in IRequest<TResponse>, and remove or rename duplicate request classes.
public record CreateOrderCommand(string Sku) : IRequest<int>;
public class CreateOrderHandler : IRequestHandler<CreateOrderCommand, int>
{
public Task<int> Handle(CreateOrderCommand request, CancellationToken ct)
=> Task.FromResult(42);
}Verify
The service type printed in the first step equals the one in the old error message, character for character.
dotnet build && dotnet run --project src/MyShop.ApiConfirmed by raw.githubusercontent.com
Cause 3 of 3
The handler is an open generic class and generic registration is off
You seeThe handler is declared like GetByIdHandler<T> : IRequestHandler<GetById<T>, T>, and the error names a closed type such as GetById`1[Customer]
Check
Open generic request handlers are not picked up by default. MediatR registers open generic implementations automatically only for notification handlers and the exception handler and action types, and since 12.4.1 the automatic registration of generic request handlers is opt-in through RegisterGenericHandlers.
grep -rnE "class \w+Handler<\w+>" --include=*.cs .Fix
Turn on generic handler registration in AddMediatR, or replace the generic handler with closed handlers for the request types you actually send.
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<CreateOrderHandler>();
cfg.RegisterGenericHandlers = true;
});Verify
Sending GetById<Customer> reaches the generic handler without the exception.
dotnet run --project src/MyShop.ApiConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Add an integration test that builds the real service provider and resolves IRequestHandler for every request type in the Application assembly.
- Keep one AssemblyMarker class in each project that holds handlers and scan by that marker, so moving handlers between projects cannot silently drop them.
- Declare each request and its handler in the same file, which makes a mismatched response type visible in review.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.