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

Consider exposing IConfiguration from IHostBuilder #1171

Closed
MV10 opened this issue Dec 26, 2019 · 2 comments
Closed

Consider exposing IConfiguration from IHostBuilder #1171

MV10 opened this issue Dec 26, 2019 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Hosting feature-request
Milestone

Comments

@MV10
Copy link

MV10 commented Dec 26, 2019

Calling Host.CreateDefaultBuilder automatically builds IConfiguration with a number of useful, very standardized rules. However, that configuration information is only available from HostBuilderContext via delegate-consuming config methods like ConfigureServices lambdas.

It would be useful if that configuration information was available either directly from IHostBuilder (or perhaps IHostBuilderContext could be exposed instead). I have scenarios where I need to call asynchronous methods during startup which require configuration information. I've run across this building a variety of different types of applications, such as those that run on Azure (for example, Key Vault calls are async), building Orleans apps (connect a cluster-client and register it for injection), various database calls during startup, and so on. I suppose this is the same reason everyone wishes we had async config methods in ASP.NET Startup.cs.

Currently I'm "stealing" a reference to configuration with a dummy call to ConfigureServices:

IConfiguration config = null;
hostBuilder.ConfigureServices((ctx, svc) => config = ctx.Configuration );
var client = await ConnectAsync(config);

It works, but it's pretty clearly a hack. I can't put the async call inside the lambda for a less hacky approach because void-returning async lambdas can't be awaited:

// no Task on this Action<>...
hostBuilder.ConfigureServices(async (ctx, svc) =>
{
    // ...without a Task, this won't actually await:
    var client = await ConnectAsync(ctx.Configuration);
});
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Configuration untriaged New issue has not been triaged by the area owner labels Dec 26, 2019
@ericstj ericstj added area-Extensions-Hosting api-suggestion Early API idea and discussion, it is NOT ready for implementation feature-request and removed area-System.Configuration untriaged New issue has not been triaged by the area owner labels Jul 7, 2020
@ericstj ericstj added this to the 6.0.0 milestone Jul 7, 2020
@maryamariyan maryamariyan added this to Triaged in ML, Extensions, Globalization, etc, POD. via automation Oct 1, 2020
@maryamariyan maryamariyan modified the milestones: 6.0.0, Future Jan 18, 2021
@maryamariyan maryamariyan moved this from Uncommitted to Future in ML, Extensions, Globalization, etc, POD. Feb 3, 2021
@greggwon
Copy link

greggwon commented Feb 4, 2022

Ultimately, all this injection just really cripples construction of services and "the app" around the fact that there is so much context wrapped into how all of this is strung together. Too much is hidden inside that is not instantly accessible. The Builder series of interfaces should really be properties of an "AppInstance" implementation so that you can extend that class/interface in your class, and then just have everything as local to that instance methods. This feels too much like a bunch of Javascript programmers who don't understand why typescript exists created all this trying to make as many types as possible disappear and become "referenced by intent" instead of "visible for intended use".

@maryamariyan
Copy link
Member

Closing since we're working on improving this workflow with issue #61634

ML, Extensions, Globalization, etc, POD. automation moved this from Future to Done Feb 8, 2022
@dotnet dotnet locked as resolved and limited conversation to collaborators Mar 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Hosting feature-request
Projects
None yet
Development

No branches or pull requests

5 participants