Question

I am new to ninject using ninject 2.0. My application is hosted in asp.net mvc.

Now i don't know how to access kernel created in my class library.

I think i should create kernel in global.aspx and load all modules in it. But how can i make it available throughout application?

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);

        IKernel kernel = new StandardKernel(new ServiceModule());
    }

Where should Kernel created above go? and how to access it?

Was it helpful?

Solution

Look on the Ninject Dojo's extensions section for Ninject.Web.Mvc. This will show you the default pattern for managing such things (go look at the source).

You can either use it as-is (most people do), or customise it to your liking.

You also tagged this with CSL. Before you do use Ninject for Service Location, have a search around for why this is considered an antipattern (the extension referred to by default does not make the Kernel public from Global - you'd have to add a public propery yourself (but the WCF integration on the other hand does)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top