Я использую ASP.Net Core 2.2 и среду / библиотеку MediatR для объектов запросов.Когда я запускаю программу, я сталкиваюсь с этим исключением:
InvalidOperationException: не найден обработчик для запроса типа MediatR.IRequestHandler 2[Store.Core.Queries.GetProductTypesQuery,System.Collections.Generic.IEnumerable
1 [Store.Core.DomainModels.ProductType]].Зарегистрируйте ваши обработчики в контейнере.
Я добавил их в свой проект Магазина (основной проект)
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();
}
}
}
Это контроллер Iиспользовал модель 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 (я думаю, что в этом нет необходимости, но я добавил ее, чтобы предоставить полную информацию)
namespace Store.Core.DomainModels
{
public class ProductType
{
public int ProductID { get; set; }
public string ProductName { get; set; }
}
}
Единственная частьдля меня подозрительным является StartUp.cs (потому что у меня есть запросы и обработчики запросов в различных проектах), но я не знаю, чего мне не хватает.