views:

818

answers:

4

I'm struggling with the documentation to figure out exactly what I need to. The documentation (to my understanding) is for 1.5 anyway.

N.B: I don't want to extend NinjectHttpApplication

I've configured it to use the NinejctControllerFactory in Application_Start() but I get a null reference exception on the KernelContainer.Kernel when it tries to create a controller. Where do I configure the Kernel if I'm not extending NinjectHttpApplication?

A: 

You still should paste the code you have so people can see where you might have gone wrong.

I think this code should be placed in your Application_Start:

ControllerBuilder.Current.SetControllerFactory(typeof(NinjectControllerFactory));
KernelContainer.Kernel = new StandardKernel(
    new AutoControllerModule(Assembly.GetExecutingAssembly();
);
Talljoe
I've pretty much given my code in that all I have so far is setting the controller factory. It didn't seem too worth it for one line! Also, this is for v1 of Ninject. I am interested in v2.
joshcomley
I don't actually see any code, or any detailed stacktrace or anything else. The solution seems obvious so I want to make sure you're at least doing the obvious. I missed the ninject 2 part (only saw 1.5 inthe body), so I've updated my answer with what I think is right for Ninject 2.
Talljoe
+3  A: 

Since you're already extending another HttpApplication-derived class, my thoughts are to just copy the relevant source code from the NinjectHttpApplication class into your extended HttpApplication class. Rather than cut and paste it, just look at the source for NinjectHttpApplication in the Ninject2 Ninject.Web.Mvc extension project here.

I would specifically copy the stuff in Application_Start() and Application_Stop() methods. The other methods for registering controllers are nice, but you can register your controllers however you wish. You'll note in the Application_Start(), the kernel is created by calling the pure virtual function CreateKernel() -- you can simply create your kernel inline right there. Additionally, note the presence of the Kernel property on the NinjectHttpApplication class -- I'd copy that into your own class as well. It would appear the intent here is that the HttpApplication-derived class effectively serves as the KernelContainer.

Disclaimer: I haven't tried this to see if it works, though I will be shortly. I've used Ninject 1.x in a web project and intend to upgrade to Ninject 2 in the near future; however, I'll probably be able to derive directly from NinjectHtppApplication. Good luck!

Peter Meyer
+2  A: 

Take a look at this blog post. It should help clarify the process you need to perform on how to configure the kernel.

http://www.kevinrohrbaugh.com/blog/2009/8/7/using-ninject-2-with-aspnet-mvc.html

Dale Ragan
This is still based on deriving from NinjectHttpApplication.
Peter Meyer
When he says extend, does he mean derive/inherit?
Dale Ragan
That's how I interpreted it. But, that's a good question.
Peter Meyer
+1  A: 

Not being able to derive from NinjectHttpApplication isn't a big deal. It's not doing too much, it's very convenient though. Peter Meyer's suggestion is the way to go. Just check out the source here. You will have to inherit from IHaveKernel though.

J.R. Garcia