When I create a web host for the main ASP.NET application, I can specify the class Startup, but not the instance.
The constructor of the Startup native class can accept a parameter that is provided through the DI. I know how to register services for DI internally ConfigureServices, but since this is a member of this class, these services are not available to the constructor of my startup class.
How to register services that will be available as a parameter of the constructor of the Startup class?
The reason is because I have to provide an instance of an object that needs to be created outside / before the web host is created, and I don't want to pass it in a global style.
Code for creating IWebHost:
this.host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<WebStartup>()
log.Debug("Run webhost");
this.host.Start();
Constructor WebStartup:
public WebStartup(IHostingEnvironment env, MyClass myClass)
{
var config = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.Build();
...
}
, MyClass (, , , WebStartup IWebHost)?