在Application_Start中访问ninject内核

发布于 2024-11-29 11:06:21 字数 1563 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2024-12-06 11:06:21

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

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

现在我应该指出,没有必要使用操作过滤器来执行此操作。在 Ninject.MVC3 中,您应该使用 BindFilter 语法,如下所示:

// 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>();
    }
}

请注意,您必须使用 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 your MvcApplication class if you need it:

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

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>();
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文