覆盖NServiceBus中的依赖项

时间:2011-10-10 10:44:29

标签: c# dependency-injection nservicebus spring.net

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

在我的EndpointConfig课程中,我正在配置一个组件:

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

(这一点没问题!)

我创建了一个新的个人资料:

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

1 个答案:

答案 0 :(得分:2)

不要在端点配置类中配置实际组件,而是考虑在处理其他NServiceBus配置文件的类中注册它 - Lite,Integration,Production。

相关问题