在我的项目中,我使用EasyNetQ库,并且我希望使用Ninject作为EasyNetQ组件的IoC容器。
我创建了一个自定义记录器,以便从EasyNetQ中记录任何内容:
public class LogNothingLogger : IEasyNetQLogger
{
...
}然后在我的主要函数中使用Ninject扩展:
public class Program
{
static void Main(string[] args)
{
// Container creation and custom logger registration
IKernel cointainer = new StandardKernel();
cointainer.Bind<IEasyNetQLogger>().To<LogNothingLogger>();
// Register Ninject as IoC Container for EasyNetQ
cointainer.RegisterAsEasyNetQContainerFactory();
// Create bus
using (IBus bus = RabbitHutch.CreateBus("host=localhost"))
{
// Do something with bus...
}
}
}但我得到了以下例外:
Ninject.ActivationException was unhandled
More than one matching bindings are available.
Matching bindings:
1) binding from IEasyNetQLogger to LogNothingLogger
2) binding from IEasyNetQLogger to method
Activation path:
2) Injection of dependency IEasyNetQLogger into parameter logger of constructor of type RabbitBus
1) Request for RabbitBus
Suggestions:
1) Ensure that you have defined a binding for IEasyNetQLogger only once.
[...]我是不是用错了这个包裹?有什么解决办法吗?
谢谢!
发布于 2014-04-16 06:38:40
正如例外情况所指出的,IEasyNetQLogger有两个绑定。
我想您正在使用的一个9对象扩展已经绑定了一个IEasyNetQLogger。
您可以使用Rebind (IBindingRoot.Rebind<IEasyNetQLogger>())覆盖IEasyNetQLogger的任何现有绑定。
但是,我也建议您了解为什么扩展已经创建了绑定,以及应该如何使用它。
您正在使用的扩展名是什么?
编辑:我浏览了一下https://github.com/mikehadlow/EasyNetQ/tree/master/Source/EasyNetQ.DI.Ninject,发现IEasyNetQLogger没有任何绑定。您确定没有定义额外的IBindingRoot.Bind<IEasyNetQLogger>().ToMethod(...)绑定吗?
也可能是NinjectAdapter.Register<IEasyNetQLogger>(Func<IEasyNetQLogger> ...)。
如果您没有这样做,那么EasyNetQ已经通过NinjectAdapter.Register<IEasyNetQLogger>(Func<IEasyNetQLogger> ...)注册了一个记录器。
和前面一样,您可以使用Rebind(..)替换绑定(必须在创建原始绑定之后执行!)或者看看它应该如何工作。
当然,您也可能只想跳过绑定,因为您只为“日志无日志记录器”创建了一个绑定。
https://stackoverflow.com/questions/23089296
复制相似问题