1

我在我的项目中使用 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.
[...]

我以错误的方式使用这个包吗?有什么解决办法吗?

谢谢!

4

1 回答 1

1

正如例外所说,. 有两个绑定IEasyNetQLogger

我想您正在使用的 ninject 扩展已经绑定了一个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(..)来替换绑定(必须在创建原始绑定之后完成!)或查看它应该如何工作。


当然,您可能也只想跳过绑定,因为您只为“无日志记录器”创建了一个...

于 2014-04-16T06:38:40.267 回答