DependencyInjectionConfig 中的 MediatR 问题与 Autofac

MediatR issue in DependencyInjectionConfig with Autofac

我在我的项目中使用 MediatR。这就是我的服务 class:

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);
        }
    }
}

这是我的模型class:

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>>();

它一直给我这个异常:

{"The requested service 'MediatR.IRequestHandler`2[[Service.DataContracts.UpdateAddressInformationCommand, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MediatR.Unit, MediatR, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency."}

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

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

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

为了更容易使用 mediatr 和 Autofac,请尝试 MediatR.Extensions https://github.com/bernos/MediatR.Extensions

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