2010-06-17 30 views
1

我寻找类似于下面的东西(http://github.com/ninject/ninject.web.mvc):Ninject: - (!MVC)ninject.web如何定期ASP.Net Web上的应用

README.markdown

这个扩展允许在Ninject核心和ASP.NET MVC项目之间进行整合。要使用它,只是让你的HttpApplication(通常在Global.asax.cs中)延长NinjectHttpApplication:

公共类YourWebApplication: NinjectHttpApplication {公共 覆盖无效OnApplicationStarted()
{// 这仅需要MVC1 RegisterAllControllersIn(“Some.Assembly.Name”); }

公众覆盖的iKernel CreateKernel(){ 返回新StandardKernel(新SomeModule(),新SomeOtherModule(), ...);

// OR, to automatically load modules: 

var kernel = new StandardKernel(); 
kernel.AutoLoadModules("~/bin"); 
return kernel; } } 

一旦你这样做,你的控制器将通过Ninject被激活,这意味着你可以在它们的构造函数(或属性或方法),要求注射暴露的依赖。

回答

1

当你没有排除它在你的问题,我必须承担你不知道的the WebForms equivalent of the one you cited

(从the Ninject extensions index on the home site链接)

+0

部分正确。我知道扩展ninject.web,但不知道如何在现有的asp.net web应用程序上实现它。 – 2010-06-25 02:48:32

+0

@No正文:http://davidhayden.com/blog/dave/archive/2008/06/20/ninjectdependencyinjectionaspnetwebpagessample.aspx涵盖了它的相当不错 - 有一个基础类的全球和页面。有没有没有涵盖的东西?如果有,问。 – 2010-06-25 07:57:17

+0

公平,但最后,海登使用Ninject.Framework.Web这是一样的新ninject.web? – 2010-06-25 10:03:13

2

只是想分享我怎么解决它使用Visual Studio 2008

对于那些你们去过www.tekpub.com的代码有点儿熟悉,是的!下面您正确的代码是从掌握ASP.NET MVC 2.0系列,以及如何使用NLOG

最少报名参考的演示:

  • Ninject.dll
  • Ninject.Web
  • NLOG .DLL

的Global.asax:

<%@ Application Language="C#" Inherits ="Ninject.Web.NinjectHttpApplication" %> 
<%@ Import Namespace="App_Code.Infrastructure.Logging"%> 
<%@ Import Namespace="Ninject.Modules"%> 
<%@ Import Namespace="Ninject"%> 

<script runat="server"> 

    void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 

    } 

    void Application_End(object sender, EventArgs e) 
    { 
     // Code that runs on application shutdown 

    } 

    void Application_Error(object sender, EventArgs e) 
    { 
     // Code that runs when an unhandled error occurs 

    } 

    void Session_Start(object sender, EventArgs e) 
    { 
     // Code that runs when a new session is started 
    } 

    void Session_End(object sender, EventArgs e) 
    { 
     // Code that runs when a session ends. 
     // Note: The Session_End event is raised only when the sessionstate mode 
     // is set to InProc in the Web.config file. If session mode is set to StateServer 
     // or SQLServer, the event is not raised. 

    } 


    protected override void OnApplicationStarted() 
    { 
     //base.OnApplicationStarted(); 

     Container.Get<ILogger>().Info("Application Started"); 
    } 

    protected override IKernel CreateKernel() 
    { 
     return Container; 
    } 

    static IKernel Container 
    { 
     get 
     { 
      return new StandardKernel(new SiteModule()); 
     } 

    } 

    class SiteModule : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<ILogger>().To<NLogger>().InSingletonScope(); 
     } 
    } 

</script> 
相关问题