gpt4 book ai didi

ChildKernel 上的 Ninject ActivationException

转载 作者:行者123 更新时间:2023-12-04 05:53:52 25 4
gpt4 key购买 nike

我收到一个 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.



所以需要显式绑定(bind) MyDataSource自我使其工作:
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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com