Getting a reference to the Startup.cs object

Here is 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(); 

In this snippet, an ASP.NET Core device creates an instance of the Startup.cs class

 .UseStartup<Startup>() 

My question is how can I get (a link) to this already created instance of the Startup object, which I can connect to my library / platform.

The context is setting up some Uber-level infrastructure and getting a link to this intersection (Startup.cs), where all requests are triggered.

+5
source share
1 answer

If your Startup implements the IStartup interface, getting a link 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. 

However, the asp.net core does not require your startup class to implement this interface. If this is not the case, it will use the adapter template and adapt your Startup class to the 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 . The Asp.net kernel will look at the methods of your startup class, find the Configure and ConfigureServices methods, and pass them to ConventionBasedStartup , which will adapt them to the IStartup interface. In this case, it is impossible to retrieve an instance of your launch class without heavy reflection, because it is not actually stored in any field (even in the closed state) of ConventionBasedStartup and is accessible only through delegate links.

In short - if you want to get an instance of your Startup class - IStartup it to the IStartup interface.

Update on how to implement the 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(); } } 
+10
source

Source: https://habr.com/ru/post/1263838/


All Articles