HostingHostBuilderExtensions.AddDefaultHostConfigurationSources(Configuration, settings.Args);
}
+ else
+ {
+ // Command line args are added even when DisableDefaults=true. If the caller didn't want settings.Args applied,
+ // they wouldn't have set them on the settings.
+ HostingHostBuilderExtensions.AddCommandLineConfig(Configuration, settings.Args);
+ }
// HostApplicationBuilderSettings override all other config sources.
List<KeyValuePair<string, string?>>? optionList = null;
internal static void AddDefaultHostConfigurationSources(IConfigurationBuilder hostConfigBuilder, string[]? args)
{
hostConfigBuilder.AddEnvironmentVariables(prefix: "DOTNET_");
- if (args is { Length: > 0 })
- {
- hostConfigBuilder.AddCommandLine(args);
- }
+ AddCommandLineConfig(hostConfigBuilder, args);
}
internal static void ApplyDefaultAppConfiguration(HostBuilderContext hostingContext, IConfigurationBuilder appConfigBuilder, string[]? args)
appConfigBuilder.AddEnvironmentVariables();
- if (args is { Length: > 0 })
- {
- appConfigBuilder.AddCommandLine(args);
- }
+ AddCommandLineConfig(appConfigBuilder, args);
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Calling IConfiguration.GetValue is safe when the T is bool.")]
static bool GetReloadConfigOnChangeValue(HostBuilderContext hostingContext) => hostingContext.Configuration.GetValue("hostBuilder:reloadConfigOnChange", defaultValue: true);
}
+ internal static void AddCommandLineConfig(IConfigurationBuilder configBuilder, string[]? args)
+ {
+ if (args is { Length: > 0 })
+ {
+ configBuilder.AddCommandLine(args);
+ }
+ }
+
internal static void AddDefaultServices(HostBuilderContext hostingContext, IServiceCollection services)
{
services.AddLogging(logging =>
Assert.Throws<InvalidOperationException>(() => builder.Services.RemoveAt(0));
}
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public void RespectsArgsWhenDisableDefaults(bool disableDefaults)
+ {
+ HostApplicationBuilder builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
+ {
+ DisableDefaults = disableDefaults,
+ Args = new string[] { "mySetting=settingValue" }
+ });
+
+ using IHost host = builder.Build();
+ IConfiguration config = host.Services.GetRequiredService<IConfiguration>();
+
+ Assert.Equal("settingValue", config["mySetting"]);
+ }
+
private static HostApplicationBuilder CreateEmptyBuilder()
{
return new HostApplicationBuilder(new HostApplicationBuilderSettings