views:

193

answers:

4

Using the NuPack addin and installing the NInject MVC 3 package results in the following compile error in the generated NinjectMVC3.cs file.

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

The sample video David Ebbo posted shows it working just fine at 09:43.

Here is the currently generated 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  A: 

Basically, MvcServiceLocator has gone away. Whenever the video was made there was a mismatch in versions, I guess.

There are excellent explanations available here and here.

The two steps that will make Ninject work are as follows. Replace NinjectMVC3 with the following (I also changed the name which isn't necessary):

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());
Jedidja
Yep! The NuPack package was based on MVC 3 Preview 1 and needs to be updated. Thanks!
Brad Wilson
I'll try to get the live package fixed later today, thanks!
David Ebbo
+4  A: 

I have fixed the package and uploaded it to the feed. It would be great if you had a chance to try it and verify that it works now. I upped the version of Ninject.MVC3 from 0.1 to 0.2 :)

David Ebbo
Should both versions Ninject.MVC (0.1 and 0.2) appear in the Add Package Reference GUI? The new one (0.2) definitely builds as expected.
Jedidja
No, it shouldn't. The UI is still in a very early shape. It's more or less just a prototype.
David Ebbo
Actually, it compiled, but trying to run the code, I get Could not load type 'System.Web.Mvc.IMvcServiceLocator' from assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Jedidja
I suspect you may have some old binaries that got left in your bin folder, or something like that.
David Ebbo
A: 

I've just installed Ninject.MVC3 0.3. I'm using ASP.NET MVC 3 Beta.

I've added this code into 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've added a call to SetupDependencyInjection() into Application_Start() function so it looks like this:

    protected void Application_Start()
    {
        SetupDependencyInjection();

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

The IProductRepository and SqlProductRepository are classes that I made in my Models folder and I've added a constructor dependency to my HomeController. Here's the code:

    private IProductRepository products;

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

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

gligoran
A: 

I just tried the NuPack Ninject Asp.Net MVC 3 package, and get an error about Ninject v2.0 needing to be referenced, at runtime - has the Dependency Resolver been built using the wrong version of Ninject? I performed a Clean and a Rebuild...

Brian Eyre