3

I am new to Ninject and am struggling to get this test to pass. (This test passed with Autofac but the behaviour seems different in Ninject).

[Test]
public void RegisterInstance_unnamed_should_return_unnamed_when_multiple_registrations()
{
    var sut = new StandardKernel();
    var instance1 = new Dependency3();
    var instance2 = new Dependency3();

    sut.Bind<Dependency3>().ToConstant(instance1).Named("instance1");
    sut.Bind<Dependency3>().ToConstant(instance2);

    sut.Get<Dependency3>("instance1").ShouldBeSameAs(instance1);
    sut.Get<Dependency3>().ShouldBeSameAs(instance2);
}

When I call the last line I get this exception message:

Ninject.ActivationException : Error activating Dependency3

No matching bindings are available, and the type is not self-bindable.

Activation path: 1) Request for Dependency3

How do I resolve a binding that is not named when there are multiple bindings?

Thanks

  • Actually i believe the exception that you receive states **More than one matching bindings are available.** If so please edit the question accordingly. – BatteryBackupUnit Apr 07 '15 at 05:27
  • Thanks, but the error message was "No matching bindings." I've updated the question with the exact wording of the exception message. – Michael Whelan Apr 07 '15 at 08:14
  • This post might answer your question: http://stackoverflow.com/questions/5997483/ninject-default-contextual-binding – CarllDev Apr 07 '15 at 08:26
  • What version of ninject are you using? I'm using 3.2.2.0. I've copied your exact test code and the error message i receive is **More than one matching bindings are available** at line `sut.Get().ShouldBeSameAs(instance2);`. I suspect in your actual test code you might have missspelled `"instance1"` at either `Bind` or `Get`. Then it would make sense that you'd get the error **No matching bindings are available(...)** at `sut.Get("instance1").ShouldBeSameAs(instance1);` – BatteryBackupUnit Apr 07 '15 at 08:35

1 Answers1

3

If you want to treat the un-named binding as a "default", you're required to add .BindingConfiguration.IsImplicit = true to the named bindings. Like so:

Bind<Dependency3>().ToConstant(instance1)
   .Named("instance1")
   .BindingConfiguration.IsImplicit = true;

Otherwise the named binding will satisfy a request without a name as well.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • Thanks for the answer. Unfortunately adding .BindingConfiguration.IsImplicit = true has had no effect and I am still getting the same exception. – Michael Whelan Apr 07 '15 at 08:15
  • Hi BatteryBackupUnit. I have accepted your answer because when I create a brand new project, targeting .Net 4.5.1 and using Ninject 3.2.2.0, adding BindingConfiguration.IsImplicit = true makes the test pass. I also get the error message you suggest, "More than one matching bindings..." What is quite baffling though, is that in my existing test project, also targeting .Net Framework 4.5.1 and using Ninject 3.2.2.0, this does not make the test pass, and I get the different error message, "No matching bindings are available..." – Michael Whelan Apr 08 '15 at 06:48
  • Not sure why there would be a difference in behaviour as the test seems quite isolated from anything else in the project. – Michael Whelan Apr 08 '15 at 06:55
  • are you really certain that you didn't misspell the name? (`.Named("...")` or `Get("...")`. Because that would explain it :) Or do you have two types `Dependency3` with the same name in your "existing test project"? – BatteryBackupUnit Apr 08 '15 at 06:57