Skip to content

Environment.GetEnvironmentVariables() called multiple times during MVC app startup #44733

@stephentoub

Description

@stephentoub

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:
image
At least the first two are both called from the same method, HostBuilder.Build:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions