简体   繁体   English

Ninject将泛型接口绑定到具有多个参数的泛型具体类

[英]Ninject binding generic interface to generic concrete class with multiple parameters

This is strange, I'm using Ninject for dependency injection and I had no problem in binding until I bound a generic interface to a generic concrete class which is shown below: 这很奇怪,我使用Ninject进行依赖项注入,并且在绑定通用接口到通用具体类之前没有任何问题,如下所示:

// * Two below bindings are like many other bindings that are OK
Bind<ISupplierAssessmentMarkBiz>()
    .To<SupplierAssessmentMarkBiz>().InRequestScope();
Bind<ISupplierAssessmentDetailBiz>()
    .To<SupplierAssessmentDetailBiz>().InRequestScope();
// * This below binding is problematic
Bind(typeof(IHierarchyEntityBiz<KnowledgeDTO>))
    .To(typeof(HierarchyEntityBiz<Knowledge, KnowledgeDTO>)).InRequestScope();

Then in my controller's constructor I have this: 然后在我的控制器的构造函数中,我有这个:

public KnowledgeController(AuthenticationService authSvc, IKnowledgeBiz knowledgeBiz,
    IHierarchyEntityBiz<KnowledgeDTO> knowledgeHierarchyBiz)
        : base(authSvc)
    {
        _knowledgeBiz = knowledgeBiz;
        _knowldegHierarchyBiz = knowledgeHierarchyBiz;
    }

So when I try to call a web api service, I get this error: 因此,当我尝试调用Web API服务时,出现以下错误:

'...KnowledgeController' does not have a default constructor.

And in output window: 并在输出窗口中:

'A first chance exception of type 'Ninject.ActivationException' occurred in Ninject.dll'

Any help would be appreciated. 任何帮助,将不胜感激。

Replacing 更换

Bind(typeof(IHierarchyEntityBiz<KnowledgeDTO>))
.To(typeof(HierarchyEntityBiz<Knowledge, KnowledgeDTO>)).InRequestScope();

with

Bind<IHierarchyEntityBiz<KnowledgeDTO>>()
.To<HierarchyEntityBiz<Knowledge, KnowledgeDTO>>().InRequestScope();

should not change anything but it might be worth a try ;-) If it doesn't help, it's at least a little bit shorter. 应该不会改变任何东西,但是可能值得一试;-)如果没有帮助,它至少要短一些。

So for your issue. 因此,对于您的问题。 Please give us the entire message of the exception. 请给我们完整的异常信息。 It should contain the activation stack, so we can see why ninject tried to instanciate a KnowledgeController . 它应该包含激活堆栈,因此我们可以了解为什么ninject试图实例化KnowledgeController

Most likely you are missing bindings for AuthenticationService or IKnowledgeBiz . 很可能您缺少AuthenticationServiceIKnowledgeBiz绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM