[英]Can I Ninject something into MvcApplication?
I'm using ASP.NET MVC 5, with Ninject 3. 我正在使用ASP.NET MVC 5和Ninject 3。
I want to have some code run when the web site starts, which will pull something from the database. 我希望在网站启动时运行一些代码,这将从数据库中提取一些内容。 I already have injection set up, and it's working fine elsewhere in the project.
我已经有注射设置,它在项目的其他地方工作正常。
I changed the Global.asax
file to look like this... 我将
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);
}
...but the VoucherCompanyServiceLogic
property is always null. ...但是
VoucherCompanyServiceLogic
属性始终为null。 I copied the two lines for this (the Inject
attribute and the property declaration) from another class where they work fine. 我从另一个类复制了这两行(
Inject
属性和属性声明),它们工作正常。
I put some Debug.WriteLine
statements in this method, and in the RegisterServices
method of the NinjectWebCommon
class, and I can see that the services are being registered before I try to use this one, so that's not the issue. 我在这个方法中放了一些
Debug.WriteLine
语句,在NinjectWebCommon
类的RegisterServices
方法中,我可以看到在我尝试使用这个之前服务正在注册,所以这不是问题。
Anyone any idea why this is always null? 任何人都知道为什么这总是空的?
The instance of MvcApplication
is created for you by ASP.Net, Ninject can do nothing to automatically inject that property. MvcApplication
的实例是由ASP.Net为您创建的,Ninject无法自动注入该属性。
As you mention that injection works elsewhere in your MVC application, I am assuming you are using one of the Nuget packages that includes a NinjectWebCommon
with all the plumbing needed for DI in an MVC application. 正如你所提到的注入在你的MVC应用程序的其他地方工作,我假设你正在使用一个Nuget包,其中包含一个
NinjectWebCommon
,其中包含了MVC应用程序中DI所需的所有管道。 The NinjectWebCommon
will be run before the Application_Start
event as it likely registers a PreApplicationStartMethod
and apart from registering your dependencies it is also configuring an IDependencyResolver
for MVC behind the scenes. NinjectWebCommon
将在Application_Start
事件之前运行,因为它可能会注册PreApplicationStartMethod
,除了注册依赖项之外,它还在IDependencyResolver
为MVC配置IDependencyResolver
。
So you could use the dependency resolver in your Application_Start
method to get an instance of that service: 因此,您可以在
Application_Start
方法中使用依赖项解析Application_Start
来获取该服务的实例:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
VoucherCompanyServiceLogic = DependencyResolver.Current.GetService<IVoucherCompanyServiceLogicInterface>();
if(VoucherCompanyServiceLogic != null)
{
VoucherCompany vc = VoucherCompanyServiceLogic.GetByID(1, 1);
}
}
But what are you planning to do with that VoucherCompany
instance? 但是你打算用这个
VoucherCompany
实例做什么? Are you aware that multiple instances of the MvcApplication
are likely to be created by ASP.Net, and Application_Start
will NOT be called for every instance? 您是否知道ASP.Net可能会创建
MvcApplication
多个实例,并且不会为每个实例调用Application_Start
? See this other SO question or read this article . 请参阅此其他问题或阅读本文 。
That means you could end with instances of the HttpApplication class where both the service and the company are null. 这意味着你可以以HttpApplication类的实例结束,其中服务和公司都是null。 If you just wanted to get the company on application startup and have that object available on subsequent events, I would set
VoucherCompany
as static, populate it on Application_Start and wouldn't even bother to save the reference to the service. 如果您只是想让公司在应用程序启动时使用并在后续事件中使用该对象,我会将
VoucherCompany
设置为静态,在Application_Start上填充它,甚至不会费心保存对服务的引用。 (Depending on what you want to achieve, there might be better options.) (根据您想要实现的目标,可能有更好的选择。)
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.