Ninject with ASP.Net and MVC Web Formats

I want to use Ninject in a project that integrates ASP.Net and ASP.Net MVC web forms. I use Ninject 2, but when I use NinjectHttpApplication from Ninject.Web.Mvc, it complains when I use things like PageBase that the kernel is not created.

I have the following in Global.asax and I'm not sure 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 anyone have this working somewhere who could share some thoughts or code with him?

+7
asp.net-mvc webforms ninject
source share
3 answers

As Ruben said, I posted a message to the Ninject Mailing list:

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

The answer is shorter, this, unfortunately, is actually impossible. However, with the custom PageBase class, you can inject properties and methods (from Nate Kohari's answer on 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); } } 
+2
source share

1) Look at the source of both Mvc and non-Mvc Ninject Extensions - the code is very short and neat

2) Go to the ninject mailing list and ask this question along with what you learned from the source. There will be an answer or patch making it easy to quickly

+1
source share

Here are some very good resources on how to use DI in web forms that I recently bookmarked:

http://aspnetresources.com/articles/ioc_and_di_with_web_forms http://aspnetresources.com/articles/real_world_ioc_and_di_with_webforms

Hope this helps.

+1
source share

All Articles