我试图使用EasyNetQ和Ninject来记录消息。我设法将Ninject设置为EasyNetQ DI(我认为),但是当一个消息到达一个没有无参数构造函数的处理程序(例如,我需要一个在那里绑定的存储库)时,它不能解析。或者至少我相信这是问题,因为我在控制台上遇到了一个非常普遍的错误。在EasyNetQ/RabbitMQ消息处理程序中使用Ninject
我告诉EasyNetQ使用Ninject像这样:
RabbitHutch.SetContainerFactory(() => new NinjectAdapter(container));
我觉得这是我需要来进行设置。 Ninject适配器是EasyNetQ的一个。
我的处理程序如下所示:
public class ProfileDeactivatedUpdateHandler : IConsume<ProfileDeactivatedUpdate>
{
private readonly IProfileRepository _profileRepository;
public ProfileDeactivatedUpdateHandler(IProfileRepository profileRepository)
{
_profileRepository = profileRepository;
}
public void Consume(ProfileDeactivatedUpdate message)
{
//Do Stuff.
}
}
如果我添加一个参数的构造函数,而是设置Ninject是可以通过服务定位器(唉),那么它的工作原理。处理程序调用的很好,我可以通过ServiceLocator找到我的存储库,所以我知道至少Ninject知道存储库。
当它试图处理消息时弹出的错误是。
System.AggregateException: One or more errors occurred. ---> System.Exception: E
xception of type 'System.Exception' was thrown.
at EasyNetQ.ReflectionHelpers.DefaultFactories`1.Get()
at EasyNetQ.ReflectionHelpers.CreateInstance[T]()
at EasyNetQ.AutoSubscribe.DefaultAutoSubscriberMessageDispatcher.Dispatch[TMe
ssage,TConsumer](TMessage message)
at EasyNetQ.RabbitBus.<>c__DisplayClass6`1.<Subscribe>b__5(T msg)
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.Exception: Exception of type 'System.Exception'
was thrown.
at EasyNetQ.ReflectionHelpers.DefaultFactories`1.Get()
at EasyNetQ.ReflectionHelpers.CreateInstance[T]()
at EasyNetQ.AutoSubscribe.DefaultAutoSubscriberMessageDispatcher.Dispatch[TMe
ssage,TConsumer](TMessage message)
at EasyNetQ.RabbitBus.<>c__DisplayClass6`1.<Subscribe>b__5(T msg)<---
感谢您回来回答这个问题。合并请求! – 2014-11-07 21:44:56