Ninject 中不存在 ServiceLocator?
我正在尝试在我的 asp.net mvc3 项目中将 Ninject 与 Common Service Locator 一起使用。我已将 Ninject.dll
和 CommonServiceLocator.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不会得到问题的答案,因为使用 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