ChildKernel 上的 Ninject ActivationException

标签 ninject ninject-extensions

我收到 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>();

最佳答案

来自ChildKernel readme :

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/

相关文章:

asp.net - Ninject 与 ASP.Net 网络表单和 MVC

c# - Ninject Singleton 范围内部控制台应用程序

c# - 注入(inject) ActivationException : Error activating IAlertManagement

asp.net-mvc - Ninject,如何通过调用 LoggerFactory.CreateLogger 注入(inject)通用 Logger<T>

c# - Ninject Providers -> 在提供者中获取另一个依赖项

inversion-of-control - Ninject 自动接线

c# - Ninject 3.0 不处理映射为 InRequestScope 的对象

c# - Ninject 工厂 + InCallScope + ContextPreservation

dependency-injection - Ninject:如何从工厂访问NamedScope的根对象