-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Closed
Copy link
Labels
Milestone
Description
When starting up a plain dotnet new mvc app, Environment.GetEnvironmentVariables() is called three times, once from HostBuilder.BuildAppConfiguration(), once from HostBuilder.BuildHostConfiguration(), and once from Microsoft.AspNetCore.Host.GenericWebHostBuilder, resulting in the environment variables being parsed multiple times and in three times as many strings being created as is really needed:

At least the first two are both called from the same method, HostBuilder.Build:
runtime/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs
Lines 119 to 134 in 162b33e
| public IHost Build() | |
| { | |
| if (_hostBuilt) | |
| { | |
| throw new InvalidOperationException("Build can only be called once."); | |
| } | |
| _hostBuilt = true; | |
| BuildHostConfiguration(); | |
| CreateHostingEnvironment(); | |
| CreateHostBuilderContext(); | |
| BuildAppConfiguration(); | |
| CreateServiceProvider(); | |
| return _appServices.GetRequiredService<IHost>(); | |
| } |
It'd be nice if the call could be made once and the results used for the various calls. (It's more challenging / problematic for Environment.GetEnvironmentVariables() to cache, but we could look at that, too, if necessary.)
Related to #44598
pentp