开发者

Ninject : ninject.web - How to apply on a regular ASP.Net Web (!MVC)

What I am looking is something similar to the below (http://github.com/ninject/ninject.web.mvc):

README.markdown

This extension allows integration between the Ninject core and ASP.NET MVC projects. To use it, just make your HttpApplication (typically in Global.asax.cs) extend NinjectHttpApplication:

public class YourWebApplication : NinjectHttpApplication { public override void OnApplicationStarted()

{ // This is only needed in MVC1 RegisterAllControllersIn("Some.Assembly.Name"); }

public override I开发者_Python百科Kernel CreateKernel() { return new StandardKernel(new SomeModule(), new SomeOtherModule(), ...);

// OR, to automatically load modules:

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

Once you do this, your controllers will be activated via Ninject, meaning you can expose dependencies on their constructors (or properties, or methods) to request injections.


Just want share how did I solved it using Visual Studio 2008

For those of you guys been to www.tekpub.com code below is kinda familiar, Yes! your correct code below is from Mastering ASP.NET MVC 2.0 series, and a demonstration of how to use NLog

Needed Reference:

  • 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>


As you didnt rule it out in your question, I have to assume you're not aware of the WebForms equivalent of the one you cited

(Linked from the Ninject extensions index on the home site)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜