为什么NuPack生成的NinjectMVC3.cs不能编译? (或ASP.NET MVC 3 Beta中的MvcServiceLocator发生了什么?)
使用NuPack插件并安装NInject MVC 3软件包会在生成的NinjectMVC3.cs
文件中产生以下编译错误。为什么NuPack生成的NinjectMVC3.cs不能编译? (或ASP.NET MVC 3 Beta中的MvcServiceLocator发生了什么?)
The name 'MvcServiceLocator' does not exist in the current context
的sample video大卫博发布显示它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
(I也改变这是没有必要的名称):
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);
}
}
和添加下列行来App_Start()
在gloabl.asax.cs
DependencyResolver.SetResolver(new NinjectResolver());
我有固定的包并将其上传到饲料。如果您有机会尝试并验证它现在可行,那将是非常好的。我把Ninject.MVC3的版本从0.1提高到0.2 :)
这两个版本Ninject.MVC(0.1和0.2)是否出现在添加包参考GUI中?新的(0.2)肯定会按预期构建。 – Jedidja 2010-10-06 20:14:01
不,它不应该。用户界面仍处于早期状态。这或多或少只是一个原型。 – 2010-10-06 20:57:52
实际上,它编译,但试图运行代码,我得到不能从程序集'System.Web.Mvc,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35加载类型'System.Web.Mvc.IMvcServiceLocator' ”。 – Jedidja 2010-10-06 21:14:24
我刚刚安装了Ninject.MVC3 0.3。我正在使用ASP.NET MVC 3 Beta。
我已经添加了这些代码融入到我的Global.asax.cs
文件:
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));
}
而且我已经加入到SetupDependencyInjection()
打电话到Application_Start()
功能,所以它看起来像这样:
protected void Application_Start()
{
SetupDependencyInjection();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
的IProductRepository
和SqlProductRepository
是我在Models文件夹中创建的类,并且我在HomeController
中添加了构造函数依赖项。这里的代码:
private IProductRepository products;
public HomeController(IProductRepository productRepository)
{
products = productRepository;
}
我已经添加了一些断点,运行应用程序,它的作品就像一个魅力。希望这可以帮助。
是的! NuPack软件包基于MVC 3 Preview 1,需要更新。谢谢! – 2010-10-06 18:18:56
我会尽力让今天晚些时候修复的实时包,谢谢! – 2010-10-06 18:21:24