Search code examples
ninjectninject-3

Disable implicit binding/injection of non explicitly bound classes in Ninject 2+


If you request an unbound object from NInject, then the default behaviour is (if a suitable constructor is available) appears to be to create an instance of the appropriate object.

I'd like to disable this behaviour (I had a difficult to debug issue because something was auto-bound instead of picking up my custom binding in a module). This question hints that it is possible, but I'm unable to find the answer from the NInject wiki.


Solution

  • Remove the SelfBindingResolver from the kernel components after creation:

    kernel.Components.RemoveAll<IMissingBindingResolver>();
    kernel.Components.Add<IMissingBindingResolver, DefaultValueBindingResolver>();