Why is the generated NinjectMVC3.cs from NuPack not compiling? (or what happened to the MvcServiceLocator in ASP.NET MVC 3 Beta?)

Using the NuPack add-on and installing the NInject MVC 3 package results in the following compilation error in the generated NinjectMVC3.cs file.

The name 'MvcServiceLocator' does not exist in the current context

David Abbo's exemplary video showed that it works great at 09 :. 43

Here is the current class:

 public class NinjectMVC3 { public static void RegisterServices(IKernel kernel) { //kernel.Bind<IThingRepository>().To<SqlThingRepository>(); } public static void SetupDependencyInjection() { // Create Ninject DI Kernel IKernel kernel = new StandardKernel(); // Register services with our Ninject DI Container RegisterServices(kernel); // Tell ASP.NET MVC 3 to use our Ninject DI Container MvcServiceLocator.SetCurrent(new NinjectServiceLocator(kernel)); } } 
+7
asp.net-mvc-3 ninject nuget
source share
3 answers

Basically, the MvcServiceLocator disappeared. Whenever a video was made, there was a mismatch in the versions, I think.

There are great explanations here and here .

The two steps that will make Ninject work are as follows. Replace NinjectMVC3 following (I also changed the name, which is not needed):

 public class NinjectResolver : IDependencyResolver { private static IKernel kernel; public NinjectResolver() { kernel = new StandardKernel(); RegisterServices(kernel); } public static void RegisterServices(IKernel kernel) { //kernel.Bind<IThingRepository>().To<SqlThingRepository>(); } public object GetService(Type serviceType) { return kernel.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return kernel.GetAll(serviceType); } } 

and add the following line to App_Start() in gloabl.asax.cs

 DependencyResolver.SetResolver(new NinjectResolver()); 
+13
source share

I installed the package and uploaded it to the feed. It would be great if you had the opportunity to try it and make sure that it works now. I raised the version of Ninject.MVC3 from 0.1 to 0.2 :)

+5
source share

I just installed Ninject.MVC3 0.3. I am using ASP.NET MVC 3 beta.

I added this code to my Global.asax.cs file:

  public static void RegisterServices(IKernel kernel) { kernel.Bind<IProductRepository>().To<SqlProductRepository>(); } public void SetupDependencyInjection() { IKernel kernel = new StandardKernel(); RegisterServices(kernel); DependencyResolver.SetResolver(new Ninject.Mvc3.NinjectServiceLocator(kernel)); } 

And I added the call to SetupDependencyInjection() to the Application_Start() function so that it looks like this:

  protected void Application_Start() { SetupDependencyInjection(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 

IProductRepository and SqlProductRepository are the classes that I created in my Mods folder, and I added a constructor dependency to my HomeController . Here is the code:

  private IProductRepository products; public HomeController(IProductRepository productRepository) { products = productRepository; } 

I added some breakpoints and launched the application and it works like a charm. Hope this helps.

+1
source share

All Articles