jbogard / MediatR

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

System.InvalidOperationException: No service for type 'MediatR.IRequestHandler`2 has been registered. #976

Closed kamhawy4 closed 5 months ago

kamhawy4 commented 8 months ago

when i execute api give me this error.

the project by core 7

swagger Screenshot 2023-11-21 111813

program.cs

Screenshot 2023-11-21 111733

controller

Screenshot 2023-11-21 111919

EstebanP-dev commented 8 months ago

How many layers do you have on your project? In this case, perhaps MediatR threw the error because it cannot read the assembly where the request handler is implemented. Instead, you can try using the layer assembly of your command handler.

gkot-softwaredeveloper commented 7 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]); });

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 14 days with no activity.

tienpmw commented 3 weeks ago

services.AddMediatR(config => config.RegisterServicesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies()));