是否可以将依赖项注入NServiceBus消息转换器?
我想在NServiceBus消息转换器中注入依赖项。。。由于变异子的生存期由NServiceBus控制(NSB需要一个无参数构造函数),构造函数注入将无法工作 有什么想法吗 更新:以下是代码:是否可以将依赖项注入NServiceBus消息转换器?,nservicebus,Nservicebus,我想在NServiceBus消息转换器中注入依赖项。。。由于变异子的生存期由NServiceBus控制(NSB需要一个无参数构造函数),构造函数注入将无法工作 有什么想法吗 更新:以下是代码: public class AddTransactionInformationToOutgoingHeaders : IMutateOutgoingTransportMessages, INe
public class AddTransactionInformationToOutgoingHeaders :
IMutateOutgoingTransportMessages,
INeedInitialization
{
private readonly IProvideTransactionInformation transactionInformationProvider;
public void Init()
{
Configure.Instance.Configurer.ConfigureComponent<AddTransactionInformationToOutgoingHeaders>(DependencyLifecycle.InstancePerCall);
}
public AddTransactionInformationToOutgoingHeaders()
{
}
public AddTransactionInformationToOutgoingHeaders(IProvideTransactionInformation transactionInformationProvider)
{
this.transactionInformationProvider = transactionInformationProvider;
}
public void MutateOutgoing(object[] messages, TransportMessage transportMessage)
{
...
}
}
public类addTransactionFormationToOutgoingHeader:
IMutateOutgoingTransportMessages,
非初始化
{
私有只读IP提供交易信息交易信息提供程序;
公共void Init()
{
Configure.Instance.Configurer.ConfigureComponent(DependencyLifecycle.InstancePerCall);
}
public addTransactionFormationToOutgoingHeaders()的
{
}
public addTransactionFormationToOutgoingHeaders(IProvideTransactionInformation TransactionFormationProvider)
{
this.TransactionFormationProvider=TransactionFormationProvider;
}
public void MutateOutgoing(对象[]消息,传输消息传输消息)
{
...
}
}
}
如果去掉空的ctor,则会从Global.asax中的ConfigureBus()调用中抛出以下错误消息:
“没有为此对象定义无参数构造函数。”非常确定构造函数和属性注入都可以工作。例外情况是什么?为了解决这个问题,我只为NServiceBus保留了一个空构造函数,然后创建了一个重载构造函数,它接受Unity管理的依赖项 它起作用了 我不知道变异基因可以这样工作。这是我第一次尝试将依赖注入其中 更新: 我通过在mutator上使用属性注入解决了这个问题:
public class AddTransactionInformationToOutgoingHeaders : IMutateOutgoingTransportMessages, INeedInitialization
{
public IProvideTransactionInformation TransactionInformationProvider { get; set; }
public void Init()
{
Configure.Instance.Configurer.ConfigureComponent<AddTransactionInformationToOutgoingHeaders>(DependencyLifecycle.InstancePerCall);
}
public void MutateOutgoing(object[] messages, TransportMessage transportMessage)
{
...
}
}
public类addTransactionFormationToOutgoingHeaders:IMutateOutgoingTransportMessages,IneInInitialization
{
公共IProvideTransactionInformation TransactionFormationProvider{get;set;}
公共void Init()
{
Configure.Instance.Configurer.ConfigureComponent(DependencyLifecycle.InstancePerCall);
}
public void MutateOutgoing(对象[]消息,传输消息传输消息)
{
...
}
}
工作得很好。听起来像是个bug。你能在这里提出一个问题吗?初始化需要一个无参数的构造函数。尝试将它移动到一个单独的类吗?Adreas,我只是使用了属性注入。INeedInitializatino获得了它所需要的无参数构造函数,我的类代码看起来更整洁!谢谢你的帮助。仅供参考,观看了您的NServiceBus PluralSite视频,太棒了!让我开始使用消息变异器。有没有计划在短时间内在PluralSite上进行中高级NSB教程?Andreas,你知道有没有使用StructureMap在构造函数中注入了某种东西的mutator的代码示例?我无法让它工作,尝试在这篇文章和其他人的建议…没关系,我让它工作。我缺少
组件。请为对象的子对象配置。阅读完整的异常详细信息是值得的…很高兴你能开始!