该程序无法找到 MediatR 查询的处理程序 ASP.Net Core

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

我正在为查询对象使用 ASP.Net Core 2.2 和 MediatR framework/library。当我 运行 我遇到这个异常的程序时:

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.

我将这些打包添加到我的商店项目(主项目)

1- MediatR 7.0.0

2- MediatR.Extensions.Microsoft.DependencyInjection

这是我的Startup.cs

services.AddMediatR(typeof(Startup));

所以这是我的查询(位于名为"Store.Core"的项目中)

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

这是我的 QueryHandler(位于另一个名为 "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();
        }

    }
}

这是我使用 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);
        }
}
}

我的 ProductType 模型(我认为这不是必需的,但我添加它是为了提供完整信息)

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

对我来说唯一可疑的部分是 StartUp.cs(因为我在不同的项目中有查询和查询处理程序)但我不知道我错过了什么。

如我所料,问题出在 Startup.cs 您添加 MediatR 服务的地方。由于我的处理程序位于单独的程序集中,因此我们应该提及该程序集名称。 我在 Startup.cs

中更改了这个
public void ConfigureServices(IServiceCollection services) {
    services.AddMediatR(typeof(Startup));
}

为此:

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

这里 "Data" 是我的程序集的名称,所有处理程序都存储在那里。

我需要使用:

services.AddMediatR(typeof(DeletePeopleCommand).GetTypeInfo().Assembly);

我有这个错误。

像个傻瓜一样,我忘了让我的处理程序实现 IRequestHandler<TRequest, TResponse>,没有它容器就不会寻找处理程序。

确保您的处理程序是 public。

你可以用这个

public void ConfigureServices(IServiceCollection services){
        services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
    }

此处 GetAssemblies() 将加载所有处理程序 类.

如果我们的RequestHandler在其他csproj中也会有问题。

我们必须在启动时找到所有这些程序集,并正确注册。