c# - 覆盖 NServiceBus 中的依赖项

标签 c# dependency-injection nservicebus spring.net

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

在我的 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 都实现了相同的接口(interface),我希望配置文件会删除旧的依赖项并改用 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 配置文件(精简版、集成、生产)的类中。

关于c# - 覆盖 NServiceBus 中的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7711643/

相关文章:

c# - 如何将SQL Server的时间数据类型转换为C#日期时间数据类型?

c# - 在 github 上托管 VisualStudio 项目的指南

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'demoRestController'

java - 如何通过 Java 注解抛出异常?

azure - 由于 Newtonsoft JsonReaderException,随机出现 NServiceBus MessageDeserializationException

c# - Utf8Json根据标记字段反序列化为类型

c# - 为什么类中的索引器属性需要一个私有(private)数组字段而不仅仅是一个告诉对象其索引的整数?

dependency-injection - "Inversion of Control"、 "Dependency inversion"和 "Decoupling"之间的区别

sql-server - 传输之间的 Nservicebus 通信

workflow-foundation - WF 和 nServiceBus 是否重叠?如何?使用哪个?