Reputation: 21
I'm running into an issue in my first IMediatR, Autofac MVC project. Help is much appreciated.. Thanks in advance !!!
Handler was not found for request of type SliceProject.Services.Query.GetUserListQuery. Container or service locator not configured properly or handlers not registered with your container.
Autofac Container Code:
builder
.RegisterAssemblyTypes(typeof(IRequest<>).Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IRequest<>)))
.AsImplementedInterfaces();
builder
.RegisterAssemblyTypes(typeof(IRequestHandler<,>).Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IRequestHandler<,>)))
.AsImplementedInterfaces();
Upvotes: 2
Views: 4376
Reputation: 13704
That's because you're telling Autofac to look in the assembly that contains the IRequestHandler<TRequest, TResponse>
type. That type lives in the MediatR assembly, so there's no chance your handlers live in that assembly.
You have to chance the registration so it look in the assembly(ies) where your handlers are defined. If they're all defined in one single assembly, pick one handler and use it as a marker type. I tried to guess the name of one of your handlers here:
builder
.RegisterAssemblyTypes(typeof(GetUserListQueryHandler).Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IRequestHandler<,>)))
.AsImplementedInterfaces();
Please also note that the registration can be made simpler with a funciton provided by Autofac, AsClosedTypesOf
. It does exactly the same thing.
builder
.RegisterAssemblyTypes(typeof(GetUserListQueryHandler).Assembly)
.AsClosedTypesOf(typeof(IRequestHandler<,>)));
Finally, and this is a bit unrelated, but why do you try to register your requests in the container? Requests are usually created by custom code and not resolved from the container. In your case, it also has no effect since you made the same mistake as for handlers, that is looking for requests in the MediatR assembly, which doesn't contain any implementations of IRequest<TResponse>
.
Upvotes: 6