NInject 3约定NInject属性未被注入(NInject 3 Conventions NInject attribute not getting injected)

编程入门 行业动态 更新时间:2024-10-17 04:57:43
NInject 3约定NInject属性未被注入(NInject 3 Conventions NInject attribute not getting injected)

对于具有以下构造的类,构造函数注入很好但不是属性。 我还需要在约定中添加什么才能使Inject属性起作用

public partial class Repo { [Inject] public ILogger Logger{get;set;} }

NInjectWebCommon.cs中的绑定

private static void RegisterServices(IKernel kernel) { //Dispose all resource after the requests ends kernel.Bind(x => x.FromAssembliesMatching("*").SelectAllClasses().BindAllInterface().Configure(scope=>scope.InRequestScope())); }

For a class with below construct, constructor injection works great but not property. What else do i need to add to the conventions to make Inject attribute work

public partial class Repo { [Inject] public ILogger Logger{get;set;} }

Bindings in NInjectWebCommon.cs

private static void RegisterServices(IKernel kernel) { //Dispose all resource after the requests ends kernel.Bind(x => x.FromAssembliesMatching("*").SelectAllClasses().BindAllInterface().Configure(scope=>scope.InRequestScope())); }

最满意答案

我假设存储库类以某种方式由实体框架实例化。 因此,如果您没有使用ninject请求repo isntance,则需要使用ninject构建现有实例。 这可以通过内核上的Inject方法完成。

var kernel = new StandardKernel(); var repo = new Repo(); kernel.Inject(repo);

I'm asuming that the repository class is instantiated by entity framework somehow. So if you are not requesting the repo isntance with ninject you need to build up the existing instance with ninject. That can be done with the Inject method on the kernel.

var kernel = new StandardKernel(); var repo = new Repo(); kernel.Inject(repo);

更多推荐

本文发布于:2023-08-05 21:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未被   属性   NInject   injected   attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!