Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject with ASP.Net webforms and MVC

I want to use Ninject in a project which combines ASP.Net webforms and ASP.Net MVC. I'm using Ninject 2, but when I use NinjectHttpApplication from Ninject.Web.Mvc it complains when I use somethings like a PageBase that the Kernel is not created.

I have the following in the Global.asax and am unsure what to add.

public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override Ninject.IKernel CreateKernel()
    {
        return new StandardKernel(new ServiceModule());
    }
}

Does somebody has this working somewhere who could share some thoughts or code on this?

like image 378
Rody Avatar asked Jun 29 '10 13:06

Rody


2 Answers

As Ruben told, I've put up a message on the Ninject Mailing list:

http://groups.google.com/group/ninject/browse_thread/thread/317fc48387399aa6

The answer in short is, it unfortunately isn't really possible. However with a custom PageBase class you can make Property and Method injection possible (from Nate Kohari's answer in the Ninject Mailing List):

public abstract class PageBase : Page
{
  public IKernel Kernel { get; private set; }
  public PageBase() { Kernel = ...; }
  public void Page_Init() { Kernel.Inject(this); }
} 
like image 115
Rody Avatar answered Oct 22 '22 01:10

Rody


1) Have a look in the source for both the Mvc and non-Mvc Ninject Extensions - the code is very short and neat

2) Go to the ninject mailing list and ask this question, together with what you've learned from the source. There'll be an answer or a patch lightening quick

like image 40
Ruben Bartelink Avatar answered Oct 22 '22 01:10

Ruben Bartelink