I am trying to do contextual binding but not able to bind concrete implementation interface as constructor argument. Ninject version: 3.2.0.0
My structure is as follows:
INotifier 1. XNotifier 2. YNotifier
IPublisher 1. APublisher 2. BPublisher
where as XNotifier and YNotifier takes constructor argument of type IPublisher.
Here is the binding I have:
Bind<INotifier>()
.To<XNotifier>()
.When(x => notifictionControl.ToLower() == "xnotification" )
.WithConstructorArgument("Publisher",ctx=>Kernel.Get<IPublisher>());
Bind<INotifier>()
.To<YNotifier>()
.When(x => notifictionControl.ToLower() == "ynotification" )
.WithConstructorArgument("Publisher", ctx => Kernel.Get<IPublisher>());
Usage:
IParameter parameter = new ConstructorArgument("Publisher", publisher);
var notifier = kernel.Get<INotifier>(parameter);
But getting following error:
Error activating INotifier No matching bindings are available, and the type is not self-bindable.
Your sample code is using a local value from the place where the binding is done. I'm speculating that this is wrong = not what you want.
The error occurs because none of the
Whencondition matches at the time when the binding is resolved. Or to put into other words: at the time when Ninject is asked to return anINotifierit will evaluate all theWhenconditions of theINotifierbindings and then resolve the one that matches.Whenconditions should only be used when during runtime sometimes you want to instantiate "A" and other times "B". If during instantiation of the kernel it's already known which to do, then you should adapt your code like so: