jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.06k stars 1.17k forks source link

"No service for type 'MediatR.IRequestHandler' has been registered." issue #984

Closed longvietcode closed 10 months ago

longvietcode commented 10 months ago

So i'm new to MediatR and I got this error, please help me, thanks! image _ Version: .NET 8 MediatR 12.2.0

This is my solution structure: image

This is my Program file: image

This is my controller file: image

This is where I implement MediatR request and request handler image

AndrewPearson commented 10 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

EstebanP-dev commented 10 months ago

Same as #976.

longvietcode commented 10 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

Same as #976.

Thanks for the comments, do you guys have the code example for how I can register IRequestHandler in my Application layer? Thanks!

AndrewPearson commented 10 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

Same as #976.

Thanks for the comments, do you guys have the code example for how I can register IRequestHandler in my Application layer? Thanks!

All you need to do is amend the code in Program.cs that calls AddMediatr. This currently takes the type 'Program' which makes Mediatr add DI registration for any request handlers that are in Program's project. As there are no request handlers in that project, none are added. Instead of passing the type Program, pass a type from the Application project which contains the handlers.

longvietcode commented 10 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

Same as #976.

Thanks for the comments, do you guys have the code example for how I can register IRequestHandler in my Application layer? Thanks!

All you need to do is amend the code in Program.cs that calls AddMediatr. This currently takes the type 'Program' which makes Mediatr add DI registration for any request handlers that are in Program's project. As there are no request handlers in that project, none are added. Instead of passing the type Program, pass a type from the Application project which contains the handlers.

It works, thank you so much

MuhammadKhalilzadeh commented 9 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

Same as #976.

Thanks for the comments, do you guys have the code example for how I can register IRequestHandler in my Application layer? Thanks!

All you need to do is amend the code in Program.cs that calls AddMediatr. This currently takes the type 'Program' which makes Mediatr add DI registration for any request handlers that are in Program's project. As there are no request handlers in that project, none are added. Instead of passing the type Program, pass a type from the Application project which contains the handlers.

It works, thank you so much

What was the type from the Application project that you passed?

gkot-softwaredeveloper commented 9 months ago

Remember that you can pass the 'array' as an argument into mediatR configuration. It seems that Program.cs assembly just contains the IMediatR <-- probably u use it only for initializing your query/command flow from controllers. However, the real handlers are in the Application Layer for example. So just add one more assembly

builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies([typeof(Program).Assembly, typeof(xxxxxx).Assembly]); });

hugodevelopr commented 8 months ago

If your handlers are in a separate project, an alternative is to register the assembly of that project explicitly.


var assemblies = Assembly.Load("YourOther.ProjectWithHandlers");

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(assemblies));
ShivaChenna commented 4 months ago

I think the issue is that you only register Mediatr request handlers from the assembly containing the class Program. The request handlers are in a different assembly, the application project.

Same as #976.

Thanks for the comments, do you guys have the code example for how I can register IRequestHandler in my Application layer? Thanks!

All you need to do is amend the code in Program.cs that calls AddMediatr. This currently takes the type 'Program' which makes Mediatr add DI registration for any request handlers that are in Program's project. As there are no request handlers in that project, none are added. Instead of passing the type Program, pass a type from the Application project which contains the handlers.

Thank you