1

i'm using EasyNetQ library in my project and I would like to use Ninject as IoC Container for EasyNetQ components.

I created a custom logger in order to log anythong from EasyNetQ:

public class LogNothingLogger : IEasyNetQLogger
{
    ...
}

And then using the Ninject extension in my main function:

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...
            }
        }
    }

But I get the following exception:

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.
[...]

Am I using this package in the wrong way? Is there any solution?

Thanks!

Ganto
  • 123
  • 1
  • 6

1 Answers1

1

As the exception says, there are two bindings for IEasyNetQLogger.

I suppose that an ninject extension you are using is already binding an IEasyNetQLogger.

You could use Rebind (IBindingRoot.Rebind<IEasyNetQLogger>()) to override any existing binding for IEasyNetQLogger.

But i would also advise you to look into why the extension is already creating a binding and how it is supposed to be used.

What is the extension you are using?

Edit: i took a glance at https://github.com/mikehadlow/EasyNetQ/tree/master/Source/EasyNetQ.DI.Ninject and i did not find any binding for IEasyNetQLogger. Are you sure you don't have defined an additional IBindingRoot.Bind<IEasyNetQLogger>().ToMethod(...) binding?

It could also be NinjectAdapter.Register<IEasyNetQLogger>(Func<IEasyNetQLogger> ...).

If you have not done so, then the EasyNetQ is already registering a logger by NinjectAdapter.Register<IEasyNetQLogger>(Func<IEasyNetQLogger> ...).

As before, you can use Rebind(..) to replace the binding (which must be done after the original binding was created!) or look into how it is supposed to work.


Of course you might also just want to skip the binding since you only created one for "log nothing logger"...

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • Hi BatteryBackupUnit, i tried with Rebind but i Still get the error. If i do not bind anything to the logger EasyNetQ keep using the default one. It is like the statement `CreateBus` is still binding its own components after ninject ones. There is a strange comment in the Estension test [link](https://github.com/mikehadlow/EasyNetQ/blob/master/Source/EasyNetQ.DI.Tests/NinjectAdapterTests.cs): `Ninject doesn't allow more than one registration of a service, it throws an exception` It seems still an open issue. – Ganto Apr 16 '14 at 14:24
  • 1
    In that case EasyNetQ is creating a binding after you have called rebind. You will need to figure out how EasyNetQ logging is supposed to be configured. You need to find out who's calling `NinjectAdapter.Register(Func<...`. Maybe there is also some way you are supposed to configure EasyNetQ - not ninject- for logging. Try removing your binding and use `IResolutionRoot.Get` - see what it returns? – BatteryBackupUnit Apr 16 '14 at 15:32