gpt4 book ai didi

c# - DependencyInjectionConfig 中的 MediatR 问题与 Autofac

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:47 24 4
gpt4 key购买 nike

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

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

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

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

它一直给我这个异常(exception):

{"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

关于c# - DependencyInjectionConfig 中的 MediatR 问题与 Autofac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32624450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com