What I am trying to accomplish
I should be able to set "IHostEnvironment.Environment" and other build-time variables for HttpApplicationHost before the configuration files, like "appsettings..json" are read. Ideally, this would be done by passing in a HostApplicationBuilderSettings object. However, HttpApplicationHost.CreateBuilder() takes no arguments.
However, there are other ways to set the Environment which should work, but do not.
BUG: Failing methods to set "IHostEnvironment.Environment" or any variable based on Microsoft.Extensions.Hosting.HostDefaults
- Setting a value to be used by ConfigurationManager
- Setting the "DOTNET_environment" environment variable
- Adding command line configuration variables
Root Cause
HttpApplicationHost.CreateBuilder() internally creates a HostApplicationBuilderSettings and populates it.
This prevents other methods from working, since "HostApplicationBuilderSettings override all other config sources." 1
Proposed Solutions
I suggest both be implemented.
- Switch to using HostDefaults that can be overridden.
- Allow the user to pass an optional HostApplicationBuilderSettings to
HttpApplicationHost.CreateBuilder().
Faulty Code
public static HttpApplicationHostBuilder CreateBuilder()
{
var config = new ConfigurationManager();
config.AddConfigurationManager();
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings()
{
Configuration = config,
ApplicationName = HostingEnvironment.SiteName,
ContentRootPath = HostingEnvironment.ApplicationPhysicalPath,
EnvironmentName = HostingEnvironment.IsDevelopmentEnvironment || IsIISExpress() ? Environments.Development : Environments.Production
});
...
Proposed Code
Based on 1
private static void UseHostingEnvironmentFallback(ConfigurationManager configuration)
{
List<KeyValuePair<string, string?>>? optionList = null;
if (configuration[HostDefaults.ApplicationKey] is null)
{
optionList ??= new List<KeyValuePair<string, string?>>();
optionList.Add(new KeyValuePair<string, string?>(HostDefaults.ApplicationKey, HostingEnvironment.SiteName));
}
if (configuration[HostDefaults.EnvironmentKey] is null)
{
optionList ??= new List<KeyValuePair<string, string?>>();
optionList.Add(new KeyValuePair<string, string?>(HostDefaults.EnvironmentKey, HostingEnvironment.IsDevelopmentEnvironment || IsIISExpress() ? Environments.Development : Environments.Production));
}
if (configuration[HostDefaults.ContentRootKey] is null)
{
optionList ??= new List<KeyValuePair<string, string?>>();
optionList.Add(new KeyValuePair<string, string?>(HostDefaults.ContentRootKey, HostingEnvironment.ApplicationPhysicalPath));
}
if (optionList is not null)
{
configuration.AddInMemoryCollection(optionList);
}
static bool IsIISExpress()
{
using var currentProcess = Process.GetCurrentProcess();
return string.Equals(currentProcess.ProcessName, "iisexpress", StringComparison.Ordinal);
}
}
public static HttpApplicationHostBuilder CreateBuilder(HostApplicationBuilderSettings? settings = null)
{
settings ??= new HostApplicationBuilderSettings();
settings.Configuration ??= new ConfigurationManager();
settings.Configuration.AddConfigurationManager();
UseHostingEnvironmentFallback(settings.Configuration);
var builder = new HostApplicationBuilder(settings);
...
To Reproduce
Attempt to set environment prior to configuration loading, when using HttpApplicationHost.CreateBuilder()
Further technical details
ASP.NET Framework Application: 4.8
Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices 2.3.0
Microsoft.AspNetCore.Hosting 2.3.0
Microsoft.Extensions.Hosting 9.0.5
What I am trying to accomplish
I should be able to set "IHostEnvironment.Environment" and other build-time variables for
HttpApplicationHostbefore the configuration files, like "appsettings..json" are read. Ideally, this would be done by passing in aHostApplicationBuilderSettingsobject. However,HttpApplicationHost.CreateBuilder()takes no arguments.However, there are other ways to set the Environment which should work, but do not.
BUG: Failing methods to set "IHostEnvironment.Environment" or any variable based on
Microsoft.Extensions.Hosting.HostDefaultsRoot Cause
HttpApplicationHost.CreateBuilder()internally creates aHostApplicationBuilderSettingsand populates it.This prevents other methods from working, since "HostApplicationBuilderSettings override all other config sources." 1
Proposed Solutions
I suggest both be implemented.
HttpApplicationHost.CreateBuilder().Faulty Code
Proposed Code
Based on 1
To Reproduce
Attempt to set environment prior to configuration loading, when using
HttpApplicationHost.CreateBuilder()Further technical details
ASP.NET Framework Application: 4.8
Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices 2.3.0
Microsoft.AspNetCore.Hosting 2.3.0
Microsoft.Extensions.Hosting 9.0.5