Property injection without using attribute [Inject] with Ninject

我们两清 提交于 2020-01-06 05:50:45

问题


I have a controller and a customized ActionInvoker. I can set the customized ActionInvoker as following,

public HomeController()
{
    this.ActionInvoker = new MyActionInvoker(..);
}

But don't want to do this in every controller (and I don't want to use a base controller either). I want to use Ninject to inject the ActionInvoker. Since ActionInvoker is part of the base controller I can't add [inject] to it. I searched a lot and tried something like

Bind<HomeController>().ToSelf().WithPropertyValue("ActionInvoker", 
   x =>x.Kernel.GetService(typeof(IActionInvoker)));

But it didn't work. Am I doing it wrong? Or any suggestion to achieve what I wanted. Thanks

Update: This works, something else was wrong. Thank you for the answers.


回答1:


Have you tried:

Bind<IActionInvoker>()
    .To<MyActionInvoker>()
    .Using<SingletonBehavior>();


Bind<HomeController>()
    .ToSelf()
    .InjectPropertiesWhere(p => p.Name == "ActionInvoker");


来源:https://stackoverflow.com/questions/5896929/property-injection-without-using-attribute-inject-with-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!