Skip to content
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

Closed
dazinator opened this issue Mar 12, 2020 · 5 comments · Fixed by #24144
Closed

Startup class activation allow additional params #19809

dazinator opened this issue Mar 12, 2020 · 5 comments · Fixed by #24144
Assignees
Labels
area-hosting Includes Hosting area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions enhancement This issue represents an ask for new feature or an enhancement to an existing one
Milestone

Comments

@dazinator
Copy link

dazinator commented Mar 12, 2020

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.

@mkArtakMSFT mkArtakMSFT added the feature-platform Deprecated: Cross-cutting issues related to ASP.NET Core as a platform label Mar 13, 2020
@Tratcher Tratcher added area-hosting enhancement This issue represents an ask for new feature or an enhancement to an existing one and removed feature-platform Deprecated: Cross-cutting issues related to ASP.NET Core as a platform labels Mar 17, 2020
@davidfowl
Copy link
Member

This makes sense to me. I’ll play with it and see if there are any rough edges

@davidfowl davidfowl self-assigned this Mar 18, 2020
@analogrelay analogrelay added this to the Backlog milestone Mar 23, 2020
@ghost
Copy link

ghost commented Jul 21, 2020

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@davidfowl
Copy link
Member

davidfowl commented Jul 21, 2020

API proposal:

public class WebHostBuilderExtensions
{
    public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Func<WebHostBuilderContext, object> startupFactory);
}

Usage

using 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

  • Gives the user control over creation of the startup class with the WebHostBuilderContext.
  • We still invoke everything via reflection, there's no startup interface here. This makes the change small and reduces the risk since the only change is how the instance is resolved.

cc @Tratcher

@davidfowl
Copy link
Member

This was merged into the rc1 milestone

@jzabroski
Copy link

Thank you very much!

@ghost ghost locked as resolved and limited conversation to collaborators Aug 21, 2020
@amcasey amcasey added the area-hosting Includes Hosting label Jun 1, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-hosting Includes Hosting area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants