4

[![enter image description here][1]][1]Im using asp.net core .net 6 and MediatR 12.0.1 and i'm not using on purpose MediatR.Extensions.Microsoft.DependencyInjection which is deprecated. In my Program.cs file i have:

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

And when im trying to ese MediatR i have error

No service for type 'MediatR.IRequestHandler' has been registred 

hire in order controller i have this error:

    // GET: api/Orders
        [HttpGet]
        public async Task<ActionResult<IEnumerable<OrderDto>>> GetOrders()
        {
            return await Mediator.Send(new GetOrderListQuery()); //error hire
       
        }

MediatR is used in controller like that:

namespace Api.Controllers
{
    [ApiController]
    public class BaseApiController : ControllerBase
    {
        private ISender _mediator = null!;

        protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetRequiredService<ISender>();
    }
}

and my Ordercontroller inherits BaseApiController

What im doing wrong?

[EDIT 1]: One important thing is that my Queries Commands and Handlers exists in second project called Application. It is working when I used this registration:

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(
    typeof(GetOrderListHandler).Assembly,
    typeof(GetOrderListQuery).Assembly
    ));

How can I do it automaticly? Project structure: [1]: https://i.stack.imgur.com/HZbA7.png

[EDIT 2]: I found quite good solution after many tests, you have to point one Class from assembly and MedaitR will register all:

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<GetOrderListHandler>(
));
mbahojlo
  • 95
  • 1
  • 10

1 Answers1

-1

Cfg => Cfg.registerservicesfromassemblies(appdomain.currentdomain.getassemblies())

Alpis
  • 1
  • 1
  • Unfortunately it doesn't work, i tried and the result is like this: Message=No service for type 'MediatR.IRequestHandler`2[Application.Catalog.Orders.GetOrderListQuery,System.Collections.Generic.List`1[Application.Catalog.Orders.OrderDto]]' has been registered. – mbahojlo Apr 01 '23 at 10:48
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '23 at 15:11