Ninject中不存在ServiceLocator?

时间:2011-10-24 17:46:00

标签: ninject

我正在尝试在我的asp.net mvc3项目中使用Ninject和Common Service Locator。我已将Ninject.dllCommonServiceLocator.NinjectAdapter.dll添加到项目中。

我在global.asax.cs文件中写了以下代码。

protected void Application_Start()
{
   AreaRegistration.RegisterAllAreas();
   ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(CreateKernel()));
   RegisterGlobalFilters(GlobalFilters.Filters);
   RegisterRoutes(RouteTable.Routes);
}

private static IKernel CreateKernel()
{
   IKernel kernel = new StandardKernel();
   kernel.Bind(typeof(IUserService)).To(typeof(UserService));
   return kernel;
}

但它没有编译,因为它找不到ServiceLocator并给我错误

"The name 'ServiceLocator' does not exist in the current context"

我在这里做错了什么?我错过了参考文献吗?

1 个答案:

答案 0 :(得分:4)

我认为你不会得到你的问题的答案,因为使用Common ServiceLocator非常罕见,特别是对于MVC3。你似乎使用的服务定位器反模式被认为是不好的做法。

几乎每个人都在使用一些IDependencyResolver实现。见https://github.com/ninject/ninject.web.mvc/wiki/MVC3