the program is not able to find handler for MediatR query ASP.Net Core

Arian Shahalami

I'm using ASP.Net Core 2.2 and MediatR framework/library for query objects. When I run the program i face to this exception:

InvalidOperationException: Handler was not found for request of type MediatR.IRequestHandler2[Store.Core.Queries.GetProductTypesQuery,System.Collections.Generic.IEnumerable1[Store.Core.DomainModels.ProductType]]. Register your handlers with the container.

I added these packaged to my Store project (main project)

1- MediatR 7.0.0

2- MediatR.Extensions.Microsoft.DependencyInjection

This is my Startup.cs

services.AddMediatR(typeof(Startup));

So this is my Query (located in a project called "Store.Core")

namespace Store.Core.Queries.Products
{
   public class GetProductTypesQuery : IRequest<IEnumerable<ProductType>> { }
}

This is my QueryHandler (located in another project called "Store.Data")

namespace Data.Queries.Products
{
    public class GetProductTypesQueryHandler : IRequestHandler<GetProductTypesQuery, IEnumerable<ProductType>>
    {
        private ApplicationDbContext _context;
        public GetProductTypesQueryHandler(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<ProductType>> Handle(GetProductTypesQuery request, CancellationToken cancellationToken)
        {
            return await _context.ProductType.OrderBy(p => p.ProductTypeID).ToListAsync();
        }

    }
}

This is the Controller I used the MediatR

namespace Store.Controllers
{
    public class HomeController : Controller
    {
        private readonly IMapper _mapper;
        private readonly IMediator _mediator;
        public HomeController(IMapper mapper, IMediator mediator)
        {
            _mapper = mapper;
            _mediator = mediator;   
        }


        public IActionResult Home() => View();

        [HttpGet]
        public async Task<IActionResult> Dishes(GetProductTypesQuery query) {
            var productTypes = await _mediator.Send(query);
            var productTypesViewModel = _mapper.Map<IEnumerable<ProductTypeVM>>(productTypes);
            return View(productTypesViewModel);
        }
}
}

my ProductType model (I Think it's not necessary but i added it in order to provide full info)

namespace Store.Core.DomainModels
{
    public class ProductType
    {
       public int ProductID { get; set; }
       public string ProductName { get; set; }
    }
}

The only part which is fishy for me is StartUp.cs (because I have queries and queries handlers in diffrent projects) but I don't know what i'm missing.

Arian Shahalami

As I guessed, the problem was the Startup.cs where u add the MediatR service. Since my Handlers were in separate assembly so we should mention that assembly name. I changed this in Startup.cs

public void ConfigureServices(IServiceCollection services) {
    services.AddMediatR(typeof(Startup));
}

To this:

public void ConfigureServices(IServiceCollection services){
    var assembly = AppDomain.CurrentDomain.Load("Data");
    services.AddMediatR(assembly);
}

Here "Data" is the name of my assembly where all Handlers are stored there.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I register and use a MediatR pipeline handler, for ASP.NET Core?

Add a generic handler for Send and Publish methods of the MediatR library in asp .net core

MediatR with ASP.NET Core DI

ASP.NET Core 6 app not able to find UseWindowsService

ASP.NET Core MediatR error: Register your handlers with the container

Mediatr unable to resolve UserManager in ASP.Net Core

ASP.NET Core Pages Handler

Asp .Net Core policy handler not triggering

Http handler in asp.net core

Asp.Net Core 3.1 MVC Run the query only once when running the program?

CQRS - Creating BaseCommandHandler using Mediatr in C#, ASP.net Core

Registering a MediatR pipeline constrained PostProcessor using ASP.NET Core DI

How to get the integer value returned in Task <int> format - Asp.net Core MVC - MediaTr

MediatR CQRS - How to deal with unexisting resources (asp.net core web api)

Getting Null Value When Input List Files in Asp .Net Core with Mediatr

How to use MediatR on Winform .net core

asp.net core 2.0 not able to post a simple form

Not able to redirect to action when using TempData in Asp.Net Core

Published Asp.Net Core App not able to save to wwwroot

Asp.Net Core - Not Able to Upload Files From Dropzone JS

Some services are not able to be constructed in ASP.NET Core

Not able to add reference to MetadataReference of CSharpCompilation in asp.net core 3.1

ASP.NET Core - AggregateException: some services are not able to be constructed

Authorization failing for custom authentication handler for ASP.NET Core 3.1?

Pass an exception to the default ASP.Net CORE error handler

ASP.NET Core map route to static file handler

ASP.NET Core Authorization Policies: Can't step into the handler?

ASP.NET Core: Razor: Post QueryString as is to a razor handler

How to create generic route handler in ASP.NET Core