C# MassTransit和Unity IOC集装箱集成
我与MassTransit和我的IoC容器(Unity)在命名注册方面存在问题。为了澄清我的问题,我有以下代码:C# MassTransit和Unity IOC集装箱集成,c#,.net,dependency-injection,unity-container,masstransit,C#,.net,Dependency Injection,Unity Container,Masstransit,我与MassTransit和我的IoC容器(Unity)在命名注册方面存在问题。为了澄清我的问题,我有以下代码: public class CommandHandlerToConsumerAdapter<T> : Consumes<T>.All where T : class, ICommand { private readonly ICommandHandler<T> m_commandHandler; //objec
public class CommandHandlerToConsumerAdapter<T> : Consumes<T>.All
where T : class, ICommand
{
private readonly ICommandHandler<T> m_commandHandler; //object responsible for performing the expected actions with the received message
public CommandHandlerToConsumerAdapter(ICommandHandler<T> _commandHandler)
{
m_commandHandler = _commandHandler; //injected property
}
public void Consume(T _message) //receives the message
{
m_commandHandler.Handle(_message); //does the logic
}
}
}
然后我希望这两个类都通过适配器类接收消息。为此,我按如下方式注册它们,将一个不同的ICommandHandler
实现注入到每个实现中:
container.RegisterType<Consumes<Command>.All, CommandHandlerToConsumerAdapter<Command>>("CommandLogic", new InjectionConstructor(new ResolvedParameter(typeof(ICommandHandler<Command>), "CommandLogic")));
container.RegisterType<Consumes<Command>.All, CommandHandlerToConsumerAdapter<Command>> ("CommandHandler", new InjectionConstructor(new ResolvedParameter(typeof(ICommandHandler<Command>), "CommandHandler")));
container.RegisterType(“CommandLogic”,新注入构造函数(新解析参数(typeof(ICommandHandler),“CommandLogic”));
container.RegisterType(“CommandHandler”,新注入构造函数(新解析参数(typeof(ICommandHandler),“CommandHandler”));
但是,通过这样做,我没有达到预期的结果,即两个类commandHandlerToConsumerapter
,都具有注入属性CommandLogic
,和commandHandlerToConsumerapter
,都具有注入属性CommandHandler
,都是,通过myCommandHandlerToConsumeradPter
适配器类,分别通过对象CommandLogic
和CommandHandler
接收类型为Command
的消息,并相应地处理它们
目前,只有最后注册的类正在接收消息并对其进行处理。有人知道我如何通过这种方法订阅相同的消息类型吗?也许我在IoC容器的注册中弄错了什么…我看不出你发布的代码有什么问题。我已经试过了,可以用
container.ResolveAll()解析这两个实例代码>。您可以发布解析命令处理程序集合和对它们的调用的代码吗
container.RegisterType<Consumes<Command>.All, CommandHandlerToConsumerAdapter<Command>>("CommandLogic", new InjectionConstructor(new ResolvedParameter(typeof(ICommandHandler<Command>), "CommandLogic")));
container.RegisterType<Consumes<Command>.All, CommandHandlerToConsumerAdapter<Command>> ("CommandHandler", new InjectionConstructor(new ResolvedParameter(typeof(ICommandHandler<Command>), "CommandHandler")));