重写 NServiceBus 中的依赖项

发布于 2024-12-09 04:05:55 字数 1491 浏览 0 评论 0原文

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

在我的 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);
    }
}

StubCommunicator 和RealCommunicator 实现相同的接口,我希望配置文件能够删除旧的依赖项并使用 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);

I'd like to use NServiceBus profiles to override the concrete classes used in the Spring.net dependency injection for use in Integration Testing.

In my EndpointConfig class, I have a component being configured:

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

(This bit is OK!)

I created a new profile:

public class StubThirdPartyProfile : NServiceBus.IProfile
{
}

And a behaviour class to implement it:

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

Both StubCommunicator and RealCommunicator implement the same interface and I was hoping that the profile would remove the old dependency and use the StubCommunicator instead but this is not the case. Is there a way to do this?

When the solution is run, I get the following error:

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 

We're using the Spring.net framework in NServicebus configured as so:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-12-16 04:05:55

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

Instead of configuring the real component in the endpoint config class, consider registering it in a class which handles the other NServiceBus profiles - Lite, Integration, Production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文