我有一个.NET Core应用程序,其中我使用.AddMediatR
扩展来注册我的命令和处理程序的程序集,遵循CQRS方法。
在Startup.cs中的ConfigureServices方法中,我使用了官方包MediatR.Extensions.Microsoft.DependencyInjection
的扩展方法,并使用了以下参数:
services.AddMediatR(typeof(AddEducationCommand).GetTypeInfo().Assembly);
以下是命令和命令处理程序类:
AddEducationCommand.cs
public class AddEducationCommand : IRequest<bool>
{
[DataMember]
public int UniversityId { get; set; }
[DataMember]
public int FacultyId { get; set; }
[DataMember]
public string Name { get; set; }
}
AddEducationCommandHandler.cs
public class AddEducationCommandHandler : IRequestHandler<AddEducationCommand, bool>
{
private readonly IUniversityRepository _repository;
public AddEducationCommandHandler(IUniversityRepository repository)
{
_repository = repository;
}
public async Task<bool> Handle(AddEducationCommand command, CancellationToken cancellationToken)
{
var university = await _repository.GetAsync(command.UniversityId);
university.Faculties
.FirstOrDefault(f => f.Id == command.FacultyId)
.CreateEducation(command.Name);
return await _repository.UnitOfWork.SaveEntitiesAsync();
}
}
当我运行执行简单的await _mediator.Send(command);
代码的REST端点时,我的日志记录显示以下错误:
Error constructing handler for request of type MediatR.IRequestHandler`2[UniversityService.Application.Commands.AddEducationCommand,System.Boolean]. Register your handlers withthe container. See the samples in GitHub for examples.
我尝试查看官方文档中的示例,但没有找到。有人知道如何配置MediatR以正常工作吗?