3

I recently updated ASP.NET MVC 3 app to Ninject 2.2.

Previously I had the following interface to implementation binding in my main app:

Bind(typeof(IMyInterface<>)).To(typeof(MyImplementation<>)).InRequestScope();

In addition, I had the following in a different assembly that was being loaded by my main app:

var arg = new ConstructorArgument("info", "something");
Bind<IMyInterface<MyClass>>().To<MyImplementation<BlogComment>>().WithParameter(arg);

This worked fine previously and the more specific implementation (the one with the argument) was being recognized. However, when I upgraded to Ninject 2.2, I received the following error:

Error activating IMyInterface{MyClass}
More than one matching bindings are available.
Activation path:
 2) Injection of dependency IMyInterface{MyClass} into parameter myParam of constructor of type SomeOtherClass
 1) Request for IMyInterface

Suggestions:
 1) Ensure that you have defined a binding for IMyInterface{MyClass} only once.

What change was made from 2.0 to 2.2 that is causing this and is there a work around?

Omar
  • 39,496
  • 45
  • 145
  • 213

1 Answers1

5

Ninject 2.2 ensures that only one matching bindings exists when resolving instances. 2.0 returned an instance of the first matching binding ignoring that there are others. But having multiple bindings if only one is requested reflects a bad configuration and can lead to hard to detect unintended behaviors.

But I see that there should be the possibility to overrule open generic bindings with more specific ones. I'll definitely look into it and it will either be added to a bugfix release or the next major release.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • 3
    Thanks, great to see open source developers answering SO questions on their respective project. – Omar Feb 17 '11 at 23:08
  • 2
    So was there an actual bug fix for this item? It's killing us right now. If not, when can we expect the next version to be released which supports this? –  Aug 08 '11 at 16:48
  • 2
    Ditto - I've lost a lot of time from this. – Luke Bennett Oct 12 '11 at 17:06
  • 2014 and still no answer? I have just come across this same issue with Ninject 3.0! From personal experience, I know Autofac has no issues with this situation. Is there some kind of workaround? – Matt Feb 05 '14 at 10:36
  • See http://stackoverflow.com/questions/21574898/ninject-3-multiple-bindings/21577792 for an answer to this question regarding Ninject 3. – BatteryBackupUnit Feb 05 '14 at 13:04