简体   繁体   English

我可以在MvcApplication中输入一些内容吗?

[英]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.

相关问题 我如何允许用户将其自定义域指向我的asp.net MVC应用程序中的子域? [如Posterous.com所见] - How can i allow the user to point their custom domains to sub domains in my asp.net MVCapplication? [As seesn in Posterous.com] 我可以使用Ninject将IKernel注入到类中 - Can I inject IKernel into class with Ninject 我可以使用ninject来动态更改实现吗? - Can I use ninject to change implementations on the fly? 我可以让ninject实例化postharp方面吗? - Can I make ninject instantiate postsharp aspects? Ninject with MVC-如何从控制器方法中的ninject手动获取对象? - Ninject with MVC - how can I manually get object from ninject inside controller method? 在哪里可以找到ninject.web.mvc.dll? - Where can I find ninject.web.mvc.dll? 使用Unity IoC时如何实现Ninject .InSingletonScope() - How can I implement Ninject .InSingletonScope() when using Unity IoC 我可以使用 Ninject 来实例化没有任何依赖的单例服务吗? - Can I use Ninject to instantiate singleton services that nothing depends on? Ninject:我可以在多个.NET API调用之间保持绑定吗? - Ninject: can I persist a binding across many .NET API calls? 如何在Ninject中将相同的依赖项绑定到许多依赖项? - How can I bind the same dependency to many dependents in Ninject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM