我正在使用ASP.NET MVC 5和Ninject 3。我希望网站启动时运行一些代码,从数据库中获取一些内容。我已经设置了注入并在项目的其他地方正常工作。我更改了Global.asax文件以使其看起来像这样...
[Inject]
public VoucherCompanyServiceLogicInterface VoucherCompanyServiceLogic { get; set; }
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
VoucherCompany vc = VoucherCompanyServiceLogic.GetByID(1, 1);
}
...但是VoucherCompanyServiceLogic
属性始终为null。我将这两行代码(Inject
属性和属性声明)从另一个类中复制过来,那个类中它们正常工作。
我在这个方法中放置了一些Debug.WriteLine
语句,并且在NinjectWebCommon
类的RegisterServices
方法中也放置了一些语句,我可以看到服务在我尝试使用此服务之前已经被注册了,所以这不是问题所在。
有人知道为什么这总是为null吗?