我对 Ninject 和一般的 DI 也很陌生。我使用 NHibernate 作为我的 MVC 应用程序的 ORM,并且对我的结果非常满意。也就是说,直到我从 Ninject 2.1 升级到 2.2。
现在,由于使用 Ninject 的内核作为资源定位器,我的 NinjectWebsiteApplication 类中出现错误。
例子:
void NinjectWebsiteApplication_BeginRequest(object sender, System.EventArgs e)
{
ILogger logger = Kernel.Get<ILogger>();
logger.Debug(“**********REQUEST BEGIN****************”);
logger.Debug(“**** URL = ” + Request.Url.AbsolutePath);
}
示例 2:
protected override void OnApplicationStarted()
{
var bootstrapper = Kernel.Get<Bootstrapper>();
bootstrapper.RegisterAllAreas();
AreaRegistration.RegisterAllAreas();
......
(More stuff here, like AutoMapper mappings, etc.)
......
}
*Bootstrapper
该类是我创建的一个类,我在其中注册我的路线、全局过滤器等。
在上述两个示例中,我都会收到有关Kernel.Get()
函数的警告,其中指出以下内容:
'Ninject.Web.Mvc.NinjectHttpApplication.Kernel' 已过时:“不要使用 Ninject 作为服务定位器”
在对此进行了几次搜索之后,普遍的共识是这是真的。
我正在尝试解决此问题,但不知道该怎么做。
我加载了最新的 Ninject.Web.Mvc NuGet 包,它在 App_Start 文件夹下创建了 NinjectMVC3 静态类。我看到他们正在引用Microsoft.Web.Infrastructure.DynamicModuleHelper
,但我看不出这适合我正在尝试做的事情。
如果有人有任何提示可以帮助我解决我的小麻烦,我将不胜感激!