English 中文(简体)
Why does the generated NinjectMVC3.cs from NuPack not compile? (or what happened to MvcServiceLocator in ASP.NET MVC 3 Beta? )
原标题:

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));
    }
}
最佳回答

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());
问题回答

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 :)

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.





相关问题
Dependency Injection Wireup Question

If there are 3 interfaces like the following public interface IWeapon { void Kill(); } public interface ISword:IWeapon { void Slice(); } public interface IShuriken: IWeapon { void Pierce(); } ...

Is it possible to use Ninject with a static property?

I have a static SessionFactory class that initializes an NHibernate session factory. Because this process is expensive (~5 sec.), I want it to be static so it s only done once, at the beginning of ...

Ninject And Connection Strings

I am very new to Ninject and am trying Ninject 2 with MVC and Linq. I have a SqlProductRepository class and all I want to know is what s the best way of passing the connectionstring in the constructor ...

Using the Ninject kernel as a Unit of Work object factory

So I m starting to use Ninject for dependency injection and I m wondering what people think of using a kernel as an object factory for Unit of Work type objects like Linq2Sql Datacontexts. I would ...

热门标签