Skip to content

HostDefaults overridden by HttpApplicationHost.CreateBuilder() #665

Description

@EmperorArthur

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Triage 🔍Label added to new issues which need Triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions