访问中的Application_Start ninject内核 [英] Accessing ninject kernel in Application_Start

查看:111
本文介绍了访问中的Application_Start ninject内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ninject和的NuGet安装MVC3扩展。我的内核设置code是在App_Start / NinjectMVC3.cs文件。一切正常的控制器很棒,但我想不出在Global.asax.cs中MvcApplication code到(正常)接口绑定方式。

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.

我最终使用一个黑客(创建返回bootstrap.kernel公共NinjectMVC3.GetKernel()方法)。然而,这将是德precated,而且必须要做到这一点,我没有看到一个合适的方式。

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.

下面是我的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();
    }
}

有两个接口,我感兴趣的首先是一个对象只是绑定到一个全局变量(ILogger为BackgroundWork)。

There are two interfaces I am interested in. The first is just binding an object to a Global variable (the ILogger for the BackgroundWork).

和第二个是一个ActionFilter。我读<一个href=\"http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/\">http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/,但我看不出它如何插入实际登记(filter.Add)。

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

推荐答案

MVC 3引入了 DependencyResolver 被填充到一个单,并Ninject扩展支持它。你可以使用在你的 MvcApplication 类,如果你需要它:

MVC 3 introduces the DependencyResolver which is populated into a singleton, and the Ninject extension supports it. You could use that in your MvcApplication class if you need it:

protected void Application_Start()
{
    // ...
    var logger = DependencyResolver.Current.GetService<ILogger>();
}

现在我应该指出,这是没有必要用行动过滤器来做到这一点。在Ninject.MVC3你应该使用 BindFilter 语法,就像这样:

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:

// Declare empty attribute
public class MyFilterAttribute : FilterAttribute { }

// Dependency module
public class MyModule : NinjectModule
{
    public override void Load()
    {
        // Other bindings
        // ...
        this.BindFilter<MyActionFilter>(FilterScope.Action, 1)
            .WhenControllerHas<MyFilterAttribute>();
    }
}

请注意,你必须使用这个,因为 BindFilter 是一个扩展方法,你也有引用 Ninject.Web.Mvc.FilterBindingSyntax 命名空间。

Note that you have to use this because BindFilter is an extension method, and you also have to reference the Ninject.Web.Mvc.FilterBindingSyntax namespace.

这篇关于访问中的Application_Start ninject内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆