Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 Preview configure for ninject

I'm giving ASP.NET MVC 3 Preview 1 a spin and want to configure ninject with it. Is the best way still to use ninject.web.mvc extension? The sample Scott Gu posts doesn't run. It throws an "Error activating IControllerFactory" exception.

like image 309
Mark Boltuc Avatar asked Dec 23 '22 00:12

Mark Boltuc


2 Answers

You don't need "Ninject.Web.Mvc" to configure Ninject in MVC 3, as I've blogged in a post titled "Dependency Injection in ASP.NET MVC 3 using Ninject".

like image 169
Shahnawaz Khan Avatar answered Dec 29 '22 04:12

Shahnawaz Khan


I believe Scott Gu's code should read...

public static void RegisterServices(IKernel kernel) 
{
    kernel.Bind<IProductRepository>().To<SqlProductRepository>();
    kernel.Bind<IControllerFactory>().To<NinjectControllerFactory>();
}

Where the NinjectControllerFactory is found in...

using Ninject.Web.Mvc;

So yes, you do still need the mvc extension for Ninject.

Perhaps there is a better/newer way to define the default controller factory in MVC 3, but that is how I did it.

There may also be some strange behavior coming from MvcServiceLocator as indicated in this post.

like image 26
Steven Hook Avatar answered Dec 29 '22 05:12

Steven Hook