简体   繁体   English

如何将已经实例化的IOC容器传递给Asp.net 5应用程序?

[英]How can I pass an already instantiated IOC container to an Asp.net 5 application?

Using .Net core I have created a console application that self hosts an asp.net 5 for endpoint support (to receive notifications, health checks, etc...). 使用.Net core,我创建了一个控制台应用程序,该应用程序自身托管了asp.net 5,以支持端点(接收通知,运行状况检查等)。

While reading about dependency injection in Asp.net 5 it shows how to use a custom IoC container, but it instantiates the container from within the Asp.net 5 ConfigureServices() method. 在阅读有关Asp.net 5中的依赖项注入时,它展示了如何使用自定义IoC容器,但是它从Asp.net 5 ConfigureServices()方法中实例化了该容器。

The problem this poses in a console application that also hosts an asp.net 5 application is that it appears that the WebHostBuilder instantiates a new instance of the Startup class, and while you can change which class is the "startup" class with .UseStartup<MySTartup>() you still can't control which instance of MyStartup Asp.net uses, which means there's no way to pass it an already setup container. 在还承载asp.net 5应用程序的控制台应用程序中引起的问题是, WebHostBuilder似乎实例化了Startup类的新实例,并且您可以使用.UseStartup<MySTartup>()来更改哪个类是“ startup”类。 .UseStartup<MySTartup>()您仍然无法控制MyStartup Asp.net的哪个实例,这意味着无法将已设置的容器传递给它。

Is there a way (without using globals) to set up an IoC container and pass it to Asp.Net 5? 有没有办法(不使用全局变量)设置IoC容器并将其传递给Asp.Net 5?

Ah, but you can. 啊,但是可以。 If I understand your problem correctly, this should provide the behavior you need: 如果我正确理解了您的问题,这应该提供您需要的行为:

When an ASP.NET 5 application starts, the Startup class is used to bootstrap the application, load its configuration settings, etc. (learn more about ASP.NET startup). 当ASP.NET 5应用程序启动时,Startup类用于引导应用程序,加载其配置设置等(了解有关ASP.NET启动的更多信息)。 However, if a class exists named Startup{EnvironmentName} (for example StartupDevelopment), and the ASPNET_ENV environment variable matches that name, then that Startup class is used instead. 但是,如果存在一个名为Startup {EnvironmentName}的类(例如StartupDevelopment),并且ASPNET_ENV环境变量与该名称匹配,则使用该Startup类。

http://docs.asp.net/en/latest/fundamentals/environments.html#startup-conventions http://docs.asp.net/en/latest/fundamentals/environments.html#startup-conventions

As for passing the container around, it's best to avoid that. 至于传递容器,最好避免这种情况。 As a last resort you can rely on the technique I used prior to working with DI-friendly frameworks: when bootstrapping the container instance at startup, use reflection to find IoC configurations in loaded assemblies, and call their methods, passing in the container. 作为最后的选择,您可以依靠在使用DI友好框架之前使用的技术:在启动时自举容器实例时,使用反射在加载的程序集中查找IoC配置,并调用它们的方法并传入容器。

Browsing Github, it appears like this is not possible. 浏览Github,看来这是不可能的。 Asp.net 5 must always be the creator of the ioc container, and the only way to share an IOC container with the rest of the application is to grab it from IWebApplication.ApplicationServices property to be used in the rest of the app. Asp.net 5必须始终是ioc容器的创建者,与其他应用程序共享IOC容器的唯一方法是从IWebApplication.ApplicationServices属性中获取它,以在其他应用程序中使用。

I raised a Github issue to hopefully get this resolved at some point. 我提出了一个Github问题 ,希望能在某个时候解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET中使用IoC容器配置单元测试? - How to configure unit tests with an IoC container in ASP.NET? 如何通过 ASP.NET Core 3 上的 IoC 从 FluentValidation 启用 CascadeMode.Stop? - How can I enable CascadeMode.Stop from FluentValidation throug IoC on ASP.NET Core 3? 为什么我应该在ASP.Net应用程序中使用IoC Container(Autofac,Ninject,Unity等)进行依赖注入? - Why should I use IoC Container (Autofac, Ninject, Unity etc) for Dependency Injection in ASP.Net Applications? ASP.NET Core 3.x - 访问 IoC 容器 - ASP.NET Core 3.x - Access IoC Container 在ASP.net解决方案中使用Unity IOC容器 - Using Unity IOC Container in ASP.net solution 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC ASP.NET Core与现有的IoC容器和环境? - ASP.NET Core with existing IoC container & environment? 具有IoC容器并发症的C#ASP.NET依赖注入 - C# ASP.NET Dependency Injection with IoC Container Complications 如何在单独的项目中使用测试设置ASP.NET API以使用一个IoC / DI容器 - How to set ASP.NET API with test in separate project to use one IoC/DI container 如何从ASP.NET MVC 5应用程序中的“区域”在IoC容器中注册类型? - How to register type in the IoC container from an “Area” in ASP.NET MVC 5 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM