Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not override content root with default #79242

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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