2010-06-17 32 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> 
相關問題