Skip to content

Commit

Permalink
Do not override content root with default (#79242)
Browse files Browse the repository at this point in the history
  • Loading branch information
halter73 authored Dec 8, 2022
1 parent ee4120e commit fb104e3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ public HostApplicationBuilder(HostApplicationBuilderSettings? settings)

if (!settings.DisableDefaults)
{
HostingHostBuilderExtensions.ApplyDefaultHostConfiguration(Configuration, settings.Args);
if (settings.ContentRootPath is null && Configuration[HostDefaults.ContentRootKey] is null)
{
HostingHostBuilderExtensions.SetDefaultContentRoot(Configuration);
}

HostingHostBuilderExtensions.AddDefaultHostConfigurationSources(Configuration, settings.Args);
}

// HostApplicationBuilderSettings override all other config sources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ public static IHostBuilder ConfigureDefaults(this IHostBuilder builder, string[]
.UseServiceProviderFactory(context => new DefaultServiceProviderFactory(CreateDefaultServiceProviderOptions(context)));
}

internal static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostConfigBuilder, string[]? args)
private static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostConfigBuilder, string[]? args)
{
SetDefaultContentRoot(hostConfigBuilder);
AddDefaultHostConfigurationSources(hostConfigBuilder, args);
}

internal static void SetDefaultContentRoot(IConfigurationBuilder hostConfigBuilder)
{
// If we're running anywhere other than C:\Windows\system32, we default to using the CWD for the ContentRoot.
// However, since many things like Windows services and MSIX installers have C:\Windows\system32 as there CWD which is not likely
Expand All @@ -219,7 +225,10 @@ internal static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostCon
new KeyValuePair<string, string?>(HostDefaults.ContentRootKey, cwd),
});
}
}

internal static void AddDefaultHostConfigurationSources(IConfigurationBuilder hostConfigBuilder, string[]? args)
{
hostConfigBuilder.AddEnvironmentVariables(prefix: "DOTNET_");
if (args is { Length: > 0 })
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,39 +229,55 @@ public void DisableDefaultIHostEnvironmentValues()
Assert.IsAssignableFrom<PhysicalFileProvider>(env.ContentRootFileProvider);
}

[Fact]
public void ConfigurationSettingCanInfluenceEnvironment()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConfigurationSettingCanInfluenceEnvironment(bool disableDefaults)
{
var tempPath = Path.GetTempPath();

using var config = new ConfigurationManager();

config.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new(HostDefaults.ApplicationKey, "AppA" ),
new(HostDefaults.EnvironmentKey, "EnvA" ),
new(HostDefaults.ContentRootKey, tempPath)
});

var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = true,
DisableDefaults = disableDefaults,
Configuration = config,
});

Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);

Assert.Equal("AppA", builder.Environment.ApplicationName);
Assert.Equal("EnvA", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);

using IHost host = builder.Build();

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvA", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
}

[Fact]
public void DirectSettingsOverrideConfigurationSetting()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void DirectSettingsOverrideConfigurationSetting(bool disableDefaults)
{
var tempPath = Path.GetTempPath();

using var config = new ConfigurationManager();

config.AddInMemoryCollection(new KeyValuePair<string, string>[]
Expand All @@ -272,23 +288,31 @@ public void DirectSettingsOverrideConfigurationSetting()

var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = true,
DisableDefaults = disableDefaults,
Configuration = config,
ApplicationName = "AppB",
EnvironmentName = "EnvB",
ContentRootPath = tempPath,
});

Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);

Assert.Equal("AppB", builder.Environment.ApplicationName);
Assert.Equal("EnvB", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);

using IHost host = builder.Build();

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvB", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
}

[Fact]
Expand Down

0 comments on commit fb104e3

Please sign in to comment.