我收到 ActivationException 消息,表示激活 IEventBroker 时出错。 MyDataSource 采用具有参数的 IEventBroker。如果我不使用子内核,就没有问题。这是怎么回事?
var kernel = new StandardKernel();
var childKernel = new ChildKernel(kernel);
var eventBroker = new EventBroker();
childKernel.Bind<IEventBroker>().ToConstant(eventBroker);
var myDS = childKernel.Get<MyDataSource>();
最佳答案
The default behavior of Ninject that classes are bound to themself if not explicit still exists. But in this case this will be done by the top most parent. This means that this class can not have any dependency defined on a child kernel. I strongly suggest to have a binding for all objects that are resolved by ninject and not to use this default behavior.
因此,您需要显式地将 MyDataSource
绑定(bind)到 self 才能使其正常工作:
var kernel = new StandardKernel();
var childKernel = new ChildKernel(kernel);
var eventBroker = new EventBroker();
childKernel.Bind<IEventBroker>().ToConstant(eventBroker);
childKernel.Bind<MyDataSource>().ToSelf();
var myDS = childKernel.Get<MyDataSource>();
关于ChildKernel 上的 Ninject ActivationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755647/