Repository navigation
Closed
Description
Describe the bug
Binding configuration with empty string as value ("") will result in null if the section it binds to comes from a nested configuration.
To Reproduce
// code
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
// we build the configuration upfront to configure stuff (logging, ..)
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.NotIncluedByDefault.json")
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls(configuration["Urls"])
.UseConfiguration(configuration)
// we add the previously built configuration manually
.ConfigureAppConfiguration(builder => builder.AddConfiguration(configuration));
await host.Build().RunAsync();
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services">Collection of service descriptors.</param>
public void ConfigureServices(IServiceCollection services)
{
// This configuration binds from the main settings files
var ok = new WorkingConfiguration();
_configuration.GetSection("BindingOk").Bind(ok);
// This configuration binds from a settings files added "manually"
var faulted = new FaultedConfiguration();
_configuration.GetSection("BindingNotOk").Bind(faulted);
services.AddSingleton(ok);
services.AddSingleton(faulted);
}
/// <summary>
/// Application configuration.
/// </summary>
/// <param name="app">The application builder.</param>
public void Configure(IApplicationBuilder app)
{
}
}
public class WorkingConfiguration
{
public string ThisBindingWillBindToString { get; set; }
}
public class FaultedConfiguration
{
public string ThisBindingWillBindToNull { get; set; }
}
// appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"BindingOk": {
"ThisBindingWillBindToString": ""
}
}
// appsettings.NotIncluedByDefault.json
{
"BindingNotOk": {
"ThisBindingWillBindToNull": ""
}
}Further technical details
- ASP.NET Core version: 6.0.100
- The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: VisualStudioProfessional 2022 17.0.2, also observed in Rider from a colleague (dont know the version) and in production (base image is mcr.microsoft.com/dotnet/aspnet:6.0)
- Include the output of
dotnet --info:
dotnet --info Output
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100\
Host (useful for support):
Version: 6.0.0
Commit: 4822e3c3aa
.NET SDKs installed:
5.0.403 [C:\Program Files\dotnet\sdk]
6.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
Reacted by Johannes Raufeisen
Activity
halter73 commentedon Mar 17, 2022
Here's a simpler repro:
test.json
Program.cs
Output
This appears to be caused by this logic in ChainedConfigurationProvider:
runtime/src/libraries/Microsoft.Extensions.Configuration/src/ChainedConfigurationProvider.cs
Lines 39 to 43 in a1bc0f3
Most ConfigurationProviders will just look for the presence of a string in the
Dictionary<string, string?> Dataand returntrueeven if thestring?isnullas long as an entry exists in the dictionary.runtime/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationProvider.cs
Lines 38 to 39 in a1bc0f3
ChainedConfigurationProvider doesn't have access to a dictionary, just the nested
IConfigurationand it assumes an empty string is equivalent to null. I'm not sure why. This seems wrong, so I'm very tempted to change this. Unfortunately, this could be pretty breaking because it would stopConfigurationandConfigurationManagerfrom falling back to the next config source if a chainedIConfigurationreturns an empty string like it does today.eerhardt commentedon Mar 17, 2022
See also #65594
maryamariyan commentedon Mar 17, 2022
If we decided it is an OK breaking change, it would make sense to fix as long as the behavior between Configuration and ConfigurationManager stays consistent.
rosebyte commentedon Aug 5, 2026
This is fixed in main by #131480
As @halter73 diagnosed above,
ChainedConfigurationProvider.TryGetused!string.IsNullOrEmpty(value), so a key that was present in the chained configuration with an empty value was reported as absent. It therefore neither bound nor shadowed earlier providers, and a chained configuration disagreed with a plainConfigurationBuildergiven the same data. It now returnsvalue is not null, so an empty string is treated as the value it is.