autofacc的DependencyInjectionConfig中的MediatR问题

本文关键字:MediatR 问题 中的 DependencyInjectionConfig autofacc | 更新日期: 2023-09-27 18:13:10

我在我的项目中使用MediatR。这是我的service类中的内容:

public class WebShopServices : IWebShopServices
{
    private readonly IMediator _mediator;
    public WebShopServices(IMediator mediator)
    {
         _mediator = mediator;
    }
    public void UpdateCustomerAddress(UpdateAddressInformationCommand updateAddressInformation)
        {
            _mediator.Send(updateAddressInformation);
        }
}

这是我的处理器

public class UpdateCustomerAddressHandler : RequestHandler<UpdateAddressInformationCommand>
{
    private readonly OnlineSalesService _client;
    private readonly IDataContractsFactory _factory;
    private readonly IParameterValidator _validator;
    public UpdateCustomerAddressHandler(OnlineSalesService client, 
        IDataContractsFactory factory, IParameterValidator validator)
    {
        _client = client;
        _factory = factory;
        _validator = validator;
    }
    protected override void HandleCore(
        UpdateAddressInformationCommand updateAddressInformation)
    {
        _validator.Validate(updateAddressInformation);
        var updateAddressCommand = 
            _factory.CreateCustomerDefaultAddressCommand(updateAddressInformation);
        try
        {
            _client.SetCustomerDefaultAddress(updateAddressCommand);
        }
        catch (Exception ex)
        {
            throw new CustomerException("Something happened with the service.", ex);
        }
    }
}
这是我的模型类:
public class UpdateAddressInformationCommand : IRequest
{
    public string CustomerNumber { get; set; }
    public AddressInformation AddressInformation { get; set; }
}

这是我在依赖注入配置中写的:

builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof (IMediator).Assembly).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof (Ping).Assembly).AsImplementedInterfaces();
builder.RegisterInstance(Console.Out).As<TextWriter>();
builder.Register<SingleInstanceFactory>(ctx => {
    var c = ctx.Resolve<IComponentContext>();
    return t => c.Resolve(t);
});
builder.Register<MultiInstanceFactory>(ctx => {
    var c = ctx.Resolve<IComponentContext>();
    return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});
builder.RegisterType<GetCustomerDetailsResultHandler>()
    .As<IRequestHandler<GetCustomerDetailsQuery,GetCustomerDetailsResult>>();
builder.RegisterType<UpdateCustomerAddressHandler>()
    .As<RequestHandler<UpdateAddressInformationCommand>>();

它一直给我这个异常:

{"请求的服务"MediatR.IRequestHandler"2 [[Service.DataContracts.UpdateAddressInformationCommand,服务,版本=1.0.0.0,文化=中性,都必须null]、[MediatR。单位,MediatR,版本=2.0.0.0,文化=中性,PublicKeyToken=null]]'尚未注册。来要避免此异常,要么注册一个组件来提供使用IsRegistered()检查服务注册,或者使用ResolveOptional()方法来解析可选依赖项。"}

有人知道如何解决这个问题吗?

autofacc的DependencyInjectionConfig中的MediatR问题

尝试通过以下方式注册irequestthandler:

builder.RegisterAssemblyTypes(assembly).As(t => t.GetInterfaces()
            .Where(i => i.IsClosedTypeOf(typeof(IRequestHandler<,>)))
            .Select(i => new KeyedService(HandlerKey, i)));

为了更容易地使用mediatr和autoface,请尝试mediatr。扩展名https://github.com/bernos/MediatR.Extensions

我通过创建一个空类UpdateAddressInformationResult解决了这个问题,并在UpdateCustomerAddressHandler中实现了接口IRequestHandler。我真的不知道如何解决RequestHandler问题。谢谢所有。