在Application_Start中访问ninject内核
我正在使用 Ninject 和随 nuget 安装的 MVC3 扩展。我的内核设置代码位于 App_Start/NinjectMVC3.cs 文件中。控制器中的一切都运行良好,但我无法弄清楚如何(正确)绑定 Global.asax.cs MvcApplication 代码中的接口。
我最终使用了一个 hack(创建一个返回 bootstrap.kernel 的公共 NinjectMVC3.GetKernel() 方法)。然而,这将被弃用,并且必须有一种我没有看到的正确方法来做到这一点。
这是我的代码:
public class LogFilterAttribute : ActionFilterAttribute
{
private IReportingService ReportingService { get; set; }
public LogFilterAttribute( IReportingService reportingService )
{
this.ReportingService = reportingService;
}
...
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters( GlobalFilterCollection filters )
{
filters.Add( new HandleErrorAttribute() );
filters.Add( new LogFilterAttribute() );
}
...
protected void Application_Start()
{
...
RegisterGlobalFilters( GlobalFilters.Filters );
// NOTE hack:
var kernel = NinjectMVC3.GetKernel();
var logger = kernel.Get<ILogger>();
var bw = new BackgroundWork(logger);
Application["BackgroundWork"] = bw;
bw.Start();
}
}
我对两个接口感兴趣。第一个是将对象绑定到全局变量(BackgroundWork 的 ILogger)。
第二个是 ActionFilter。我读了 http:// www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/,但我不明白它是如何实现的插入实际注册(filter.Add)。
如果可以避免的话,我不想使用属性注入。
关于执行此操作的正确方法有什么想法吗? 谢谢
I am using Ninject and the MVC3 extension installed with nuget. My kernel setup code is in the App_Start/NinjectMVC3.cs file. Everything works great in controllers, but I can't figure out how to (properly) bind interfaces in the Global.asax.cs MvcApplication code.
I ended up using a hack (creating a public NinjectMVC3.GetKernel() method that returns bootstrap.kernel). However, that will be deprecated, and there must be a proper way to do this that I am not seeing.
Here is my code:
public class LogFilterAttribute : ActionFilterAttribute
{
private IReportingService ReportingService { get; set; }
public LogFilterAttribute( IReportingService reportingService )
{
this.ReportingService = reportingService;
}
...
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters( GlobalFilterCollection filters )
{
filters.Add( new HandleErrorAttribute() );
filters.Add( new LogFilterAttribute() );
}
...
protected void Application_Start()
{
...
RegisterGlobalFilters( GlobalFilters.Filters );
// NOTE hack:
var kernel = NinjectMVC3.GetKernel();
var logger = kernel.Get<ILogger>();
var bw = new BackgroundWork(logger);
Application["BackgroundWork"] = bw;
bw.Start();
}
}
There are two interfaces I am interested in. The first is just binding an object to a Global variable (the ILogger for the BackgroundWork).
And the second is for an ActionFilter. I read http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/, but I don't see how it plugs into the actual registration (filter.Add).
I don't want to use the Property Inject if I can avoid it.
Any thoughts on the proper way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MVC 3 引入了填充到单例中的 DependencyResolver,并且 Ninject 扩展支持它。如果需要,您可以在
MvcApplication
类中使用它:现在我应该指出,没有必要使用操作过滤器来执行此操作。在 Ninject.MVC3 中,您应该使用
BindFilter
语法,如下所示:请注意,您必须使用
this
因为BindFilter
是一个扩展方法,并且您还必须引用Ninject.Web.Mvc.FilterBindingSyntax
命名空间。MVC 3 introduces the
DependencyResolver
which is populated into a singleton, and the Ninject extension supports it. You could use that in yourMvcApplication
class if you need it:Now I should point out that it is unnecessary to do this with action filters. In Ninject.MVC3 you are supposed to use the
BindFilter
syntax, like so:Note that you have to use
this
becauseBindFilter
is an extension method, and you also have to reference theNinject.Web.Mvc.FilterBindingSyntax
namespace.