MediatR 错误:向容器注册您的处理程序

Posted

技术标签:

【中文标题】MediatR 错误:向容器注册您的处理程序【英文标题】:MediatR error: Register your handlers with the container 【发布时间】:2021-01-19 19:50:00 【问题描述】:

我有一个 .Net Core 应用程序,我在其中使用 .AddMediatR 扩展名为我的命令和处理程序注册程序集

在 Startup.cs 的 ConfigureServices 中,我使用了扩展方法 来自官方包 MediatR.Extensions.Microsoft.DependencyInjection 具有以下内容 参数:

startup.cs

 services.AddBLL();

DependencyInjection.cs

public static IServiceCollection AddBLL(this IServiceCollection services)
    
        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        services.AddMediatR(Assembly.GetExecutingAssembly());
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehaviour<,>));
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));

        return services;
    

commandhandler类如下: ListEpostaHesapariHandler.cs

public class ListEpostaHesaplariRequest : IRequest<ResultDataDto<List<ListEpostaHesaplariDto>>>

    public FiltreEpostaHesaplariDto Model  get; set; 


public class ListEpostaHesaplariHandler : IRequestHandler<ListEpostaHesaplariRequest, ResultDataDto<List<ListEpostaHesaplariDto>>>

    private readonly IBaseDbContext _context;
    private readonly IMapper _mapper;

    public ListEpostaHesaplariHandler(IBaseDbContext context, IMapper mapper)
    
        _context = context;
        _mapper = mapper;
    

    public async Task<ResultDataDto<List<ListEpostaHesaplariDto>>> Handle(ListEpostaHesaplariRequest request, CancellationToken cancellationToken)
    
        await Task.Delay(1);

        var resultDataDto = new ResultDataDto<List<ListEpostaHesaplariDto>>()  Status = ResultStatus.Success ;

        try
        
            var foo = _context.EpostaHesaplari;

            var filtreliEpostaHesaplari = Filtre(request.Model, foo);

            var epostaHesaplariDto = _mapper.Map<List<ListEpostaHesaplariDto>>(filtreliEpostaHesaplari);

            resultDataDto.Result = epostaHesaplariDto;
        
        catch (Exception ex)
        
            resultDataDto.Status = ResultStatus.Error;
            resultDataDto.AddError(ex.Message);
        

        return resultDataDto;
    

这是我使用 MediatR 的控制器

BaseController.cs

  public abstract class BaseController : Controller

    private IMediator _mediator;
    public static int? birimIDD;
    protected IMediator Mediator
    
        get
        
            return _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
        
    

HomeController.cs

[HttpPost]
    public async Task<IActionResult> MailGonderAsync()
    

        FiltreEpostaHesaplariDto filtre = new FiltreEpostaHesaplariDto();
        filtre.Durum = true;
        var req = new ListEpostaHesaplariRequest()  Model = filtre ;
        var response = await Mediator.Send(req);
    

到目前为止没有问题 await Mediator.Send(req);此响应即将成功

问题从这里开始

send.cs(在类业务逻辑层)

public class EmailSend : IEmailSingleSender

    private readonly IMediator _mediator;

    public EmailSend(IMediator mediator)
    
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

    

    public async Task Send(EmailParamsDto emailParamsDto)
    
        try
        
            FiltreEpostaHesaplariDto filtre = new FiltreEpostaHesaplariDto();
            filtre.Durum = true;
            var req = new ListEpostaHesaplariRequest()  Model = filtre ;
            var response = await _mediator.Send(req);

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Port = _emailSetting.Port;
            smtpClient.Host = _emailSetting.Server;
            smtpClient.EnableSsl = _emailSetting.UseSsl;
            NetworkCredential networkCredential = new NetworkCredential();
            networkCredential.UserName = _emailSetting.UserName;
            networkCredential.Password = _emailSetting.Password;
            smtpClient.Credentials = networkCredential;
            MailMessage mail = new MailMessage();
            mail.IsBodyhtml = _emailSetting.IsHtml;
            mail.From = new MailAddress(_emailSetting.UserName, emailParamsDto.Subject);
            mail.To.Add(new MailAddress(emailParamsDto.Receiver));
            //mail.CC.Add(_emailSetting.AdminMail);
            mail.Body = emailParamsDto.Body;
            mail.Subject = emailParamsDto.Subject;
            mail.Priority = emailParamsDto.MailPriority;

            await smtpClient.SendMailAsync(mail);
        
        catch (Exception ex)
        

            throw ex;
        
    

当这行在 Send 方法中执行时,我得到了异常:

var response = await _mediator.Send(req); 

为请求类型构建处理程序时出错 MediatR.IRequestHandler2[IUC.BaseApplication.BLL.Handlers.Yonetim.EpostaHesaplariHandlers.ListEpostaHesaplariRequest,IUC.BaseApplication.COMMON.Models.ResultDataDto1[System.Collections.Generic.List`1[IUC.BaseApplication.BLL.Models.Yonetim.EpostaHesaplariDto.ListEpostaHesaplariDto]]]。 向容器注册您的处理程序。查看 GitHub 中的示例 例如。

【问题讨论】:

问题解决了吗?我遇到了同样的问题,请指教。 【参考方案1】:
services.AddMediatR(Assembly.GetExecutingAssembly());

你所有的处理程序和命令都在你传递的这个程序集中?

【讨论】:

以上是关于MediatR 错误:向容器注册您的处理程序的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core MediatR错误:使用容器注册处理程序

在 .net 核心应用程序中加载标准库

如何为 ASP.NET Core 注册和使用 MediatR 管道处理程序?

使用 void/Task 响应注册 MediatR 管道

向 MediatR 行为管道添加验证?

MediatR - 处理查询中的当前用户