Ninject 中不存在 ServiceLocator?

发布于 2024-12-12 02:38:18 字数 822 浏览 3 评论 0原文

我正在尝试在我的 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"

我在这里做错了什么?我缺少参考吗?

I am trying to use Ninject with Common Service Locator in my asp.net mvc3 project. I've added Ninject.dll and CommonServiceLocator.NinjectAdapter.dll to the project.

I wrote following code in my global.asax.cs file.

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;
}

But it does not compile because it couldn't find ServiceLocator and giving me the error

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

What am I doing wrong here? Am I missing a reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2024-12-19 02:38:18

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

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

I think you won't get an answer to your question because using the Common ServiceLocator is very uncommon especially for MVC3. The service locator anti-pattern you seem to use is considered bad practice.

Almost everyone is using some IDependencyResolver implementation instead. See https://github.com/ninject/ninject.web.mvc/wiki/MVC3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文