Reputation: 1129
I'm using a Ninject module to bind different types to their corresponding interfaces.
Injection will take place inside a class's constructor. The problem is that the class has another constructor with a signature including Func.
Ninject is confused and throws this at me:
Error activating ClassTest using implicit self-binding of ClassTest. Several constructors have the same priority. Please specify the constructor using ToConstructor syntax or add an Inject attribute.
See below how the binding is done and how I do the injection:
this.Bind<InterfaceA>().To<ClassA>();
...
public class ClassTest
{
public ClassTest(InterfaceA a)
{
}
public ClassTest(Func<ClassB> funcB)
{
}
}
...
var giveMeTest = kernel.Get<ClassTest>(); // exception thrown
}
It seems that Func is the culprit here, can you please explain me why Ninject gets confused?
Thanks
Upvotes: 2
Views: 4157
Reputation: 32725
Best you delete all unused constructors. There is no reason for adding constructors that are never used. If you really need multiple constructors then you have to tell Ninject which one to pick, e.g.:
Bind<ClassTest>().ToConstructor(x => new ClassTest(x.Inject<InterfaceA>())).Named("A");
Bind<ClassTest>().ToConstructor(x => new ClassTest(x.Inject<Func<ClassB>>())).Named("B");
kernel.Get<ClassTest>("A");
kernel.Get<ClassTest>("B");
Upvotes: 8