使用NuPack插件并安装NInject MVC 3程序包会在生成的NinjectMVC3.cs文件中导致以下编译错误.
当前上下文中不存在名称“MvcServiceLocator”
sample video David Ebbo发布的显示为working just fine at 09:43.
这是当前生成的类:
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)); } }
解决方法
基本上,MvcServiceLocator已经消失了.我想,每当制作视频时,版本都会出现版本不匹配的情况.
使Ninject工作的两个步骤如下.用以下内容替换NinjectMVC3(我也更改了不必要的名称):
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); } }
并将以下行添加到gloabl.asax.cs中的App_Start()
DependencyResolver.SetResolver(new NinjectResolver());