重写NServiceBus中的依赖

本文关键字:依赖 NServiceBus 重写 | 更新日期: 2023-09-27 18:11:01

我想使用NServiceBus配置文件来覆盖在Spring.net依赖注入中使用的具体类,以便在集成测试中使用。

在我的EndpointConfig类中,我有一个组件正在配置:

NServiceBus.Configure.Instance.Configurer.ConfigureComponent<RealCommunicator>(ComponentCallModelEnum.None);

(这位是OK的!)

我创建了一个新的配置文件:

public class StubThirdPartyProfile : NServiceBus.IProfile
{
}

和一个行为类来实现它:

public class StubThirdPartyBehaviour : IHandleProfile<StubThirdPartyProfile>
{
    public void ProfileActivated()
    {
        Configure.Instance.Configurer.ConfigureComponent<StubCommunicator>(ComponentCallModelEnum.None);
    }
}

StubCommunicatorRealCommunicator都实现了相同的接口,我希望配置文件将删除旧的依赖并使用StubCommunicator,但事实并非如此。有办法做到这一点吗?

解决方案运行时,我得到以下错误:

Spring.Objects.Factory.UnsatisfiedDependencyException: 
Error creating object with name 'Namespace.CommandHandler' :
Unsatisfied dependency expressed through object property 'Communicator': 
There are 2 objects of Type [Namespace.ICommunicator] for autowire by type, 
  when there should have been just 1 to be able to autowire property 'Communicator' of object 

我们在NServicebus中使用Spring.net框架,配置如下:

Configure.With().SpringFrameworkBuilder()
                .XmlSerializer().Log4Net()
                .MsmqTransport()
                .IsTransactional(true);

重写NServiceBus中的依赖

与其在端点配置类中配置真正的组件,不如考虑将其注册在处理其他NServiceBus配置文件(Lite、Integration、Production)的类中。