-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Startup class activation allow additional params #19809
Comments
This makes sense to me. I’ll play with it and see if there are any rough edges |
Thanks for contacting us. |
API proposal:public class WebHostBuilderExtensions
{
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Func<WebHostBuilderContext, object> startupFactory);
} Usageusing System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
public class Program
{
public static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHost(builder =>
{
builder.UseStartup(context => new Startup());
})
.Build();
await host.RunAsync();
}
}
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
} Notes
cc @Tratcher |
This was merged into the rc1 milestone |
Thank you very much! |
Startup classes cannot be activated with additional constructor params
When using a startup class, suppose I want my constructor to have some arbitrary additional parameters- like an ILogger, or an IOptions object. At the moment this is not allowed because the host builder doesnt know how to satisfy those additional constructor parameters when it activates the startup class. However in other situations such as UseMiddleware you can supply those additional parameter values to be used when activating the class, at the point of registering it. You cant do that currently when registering a Startup class. This means if in startup.cs you want to use something like an ILogger that you created in program.cs, you have to resort to accessing it in a static field like Program.Logger etc.
Describe the solution you'd like
The equivalent of
.UseStartup<Startup>(logger, foo, bar)
When the startup class is activated, any constructor parameters that the host doesn't natively know how to resolve, will be matched from the additional object instances supplied at the point of registering the startup class. This is a pattern found elsewhere in certain places in the framework.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: