我正在将 Ninject 和 Ninject.Web 程序集与 Web 表单应用程序一起使用。在 global.asax 文件中,我指定了如下绑定:
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
// Vendor Briefs.
kernel.Bind<IVendorBriefRepository>().To<VendorBriefRepository>().InRequestScope();
kernel.Bind<IVendorBriefController>().To<VendorBriefController>().InRequestScope();
// Search Services.
kernel.Bind<ISearchServicesController>().To<SearchServicesController>().InRequestScope();
kernel.Bind<ISearchServicesRepository>().To<SearchServicesRepository>().InRequestScope();
// Error Logging
kernel.Bind<IErrorLogEntryController>().To<ErrorLogEntryController>().InRequestScope();
kernel.Bind<IErrorLogEntryRepository>().To<ErrorLogEntryRepository>().InRequestScope();
return kernel;
}
}
然后在我的页面中我只需让它们继承Ninject.Web.PageBase
。然后我可以在页面后面的代码上设置属性并将[inject]
属性覆盖它。
[inject]
public IVendorBriefController vendorBriefController { get; set; }
这很好用。但现在我需要在 Global.asax 文件本身中进行一些依赖项注入。我需要一个实例IErrorLogEntryController
in my Application_Error
事件。如何解决这个问题并使用我为抽象类型指定的绑定?
protected void Application_Error(object sender, EventArgs e)
{
IErrorLogEntryController = ???;
}
只需在您的 Global 类中做同样的事情
[Inject]
public IErrorLogEntryController ErrorLogEntryController { get; set; }
NinjectHttpApplication 在内核创建后注入自身。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)