获取Startup.cs对象的参考

编程入门 行业动态 更新时间:2024-10-26 09:27:40
本文介绍了获取Startup.cs对象的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是标准ASP.NET Core应用程序的框架:

Here's the skeleton of a standard ASP.NET Core application:

var config = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); var host = new WebHostBuilder() .UseConfiguration(config) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run();

在这一段中,ASP.NET Core设备实例化了Startup.cs类的实例

In this piece the ASP.NET Core apparatus instantiates an instance of Startup.cs class

.UseStartup<Startup>()

我的查询是如何保留(引用)该Startup对象的这个实例化实例,然后将其插入到Library/Framework中.

My query is how can I get hold (reference) of this already instantiated instance of Startup object that I can plug into my Library/Framework.

上下文是建立一些Uber级别的框架,并获取所有请求都在其中启动的结点(Startup.cs)的引用.

Context is to setup some Uber level framework and get a reference of this junction (Startup.cs) where all the requests are getting initiated.

推荐答案

如果您的Startup实现了IStartup接口,则引用它很容易:

If your Startup implements IStartup interface, getting reference to it is easy:

var host = new WebHostBuilder() .UseConfiguration(config) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); var startup = host.Services.GetService(typeof(IStartup)); // or from any other part of code using IServiceProvider.

但是,asp core不需要要求您的启动类即可实现此接口.如果不是,它将使用适配器模式并将Startup类调整为IStartup接口.您仍然会有IStartup的实例,但它不是您的Startup类.相反,它将是ConventionBasedStartup的实例. Asp核心将探索您的启动类的方法,找到Configure和ConfigureServices方法,并将它们传递给ConventionBasedStartup,这将使它们适应IStartup接口.在这种情况下,不可能在没有大量反思的情况下检索启动类的实例,因为它实际上没有存储在ConventionBasedStartup的任何字段中(甚至是私有的),并且只能通过委托引用来访问.

However, asp core does not require your startup class to implement this interface. If it does not - it will use adapter pattern and adapt your Startup class to IStartup interface. You will still have an instance of IStartup, but it will not be your Startup class. Instead it will be an instance of ConventionBasedStartup. Asp core will explore methods of your startup class, find Configure and ConfigureServices methods and will pass them to ConventionBasedStartup which will adapt them to IStartup interface. In this case, it's not possible to retrieve instance of your startup class without heavy reflection, because it's not actually stored in any field (even in private) of ConventionBasedStartup and is only reachable through delegate references.

长话短说-如果要获取Startup类的实例-使其实现IStartup接口.

Long story short - if you want to get instance of your Startup class - make it implement IStartup interface.

有关如何实现IStartup接口的更新:

Update about how to implement IStartup interface:

public class Startup : IStartup { public Startup(IHostingEnvironment env) { // constructor as usual var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } public void Configure(IApplicationBuilder app) { app.UseMvc(); // resolve services from container var env = (IHostingEnvironment) app.ApplicationServices.GetService(typeof(IHostingEnvironment)); var logger = (ILoggerFactory)app.ApplicationServices.GetService(typeof(ILoggerFactory)); logger.AddConsole(Configuration.GetSection("Logging")); logger.AddDebug(); // etc } public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); // etc // return provider return services.BuildServiceProvider(); } }

更多推荐

获取Startup.cs对象的参考

本文发布于:2023-11-14 14:05:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Startup   cs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!