Description
ConfigurationManager.OpenMappedExeConfiguration produces a Configuration object whose SectionGroups collection yields null entries when the config file contains a element with a name attribute containing a forward slash (/).
This causes a NullReferenceException for any code that enumerates config.SectionGroups, including the pattern shown in Microsoft's own documentation example.
Reproduction Steps
using System.Configuration;
var xml = """
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="a/b" />
</configSections>
</configuration>
""";
var tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile, xml);
try
{
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = tmpFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(
fileMap, ConfigurationUserLevel.None);
Console.WriteLine($"SectionGroups.Count = {config.SectionGroups.Count}");
foreach (ConfigurationSectionGroup group in config.SectionGroups)
{
Console.WriteLine($"Group name: {group.Name}");
}
}
finally
{
File.Delete(tmpFile);
}
Project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="10.0.3" />
</ItemGroup>
</Project>
run with dotnet run.
Expected behavior
Consistent info between SecionGroups.Count and non-null entries in the collection. The SectionGroups enumerator skips entries it cannot resolve rather than yielding null.
Actual behavior
SectionGroups.Count = 1
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Program.<Main>$(String[] args) in Program.cs:line 21
Regression?
Unknown.
Known Workarounds
Add a null check when enumerating section groups:
foreach (ConfigurationSectionGroup group in config.SectionGroups)
{
if (group is null) continue;
// ...
}
Configuration
- .NET SDK 10.0.103
- System.Configuration.ConfigurationManager NuGet package 10.0.3
- Reproduced on Linux x64 (WSL2) and Windows x64
Other information
The root cause appears to be that ConfigurationManager uses / as an internal path separator for section group hierarchies (e.g., system.web.extensions/scripting/webServices). When a <sectionGroup> element has a name containing /, the internal lookup by path fails to resolve the group record, returning null — but the count and enumerator still report it as present.
Description
ConfigurationManager.OpenMappedExeConfiguration produces a Configuration object whose SectionGroups collection yields null entries when the config file contains a element with a name attribute containing a forward slash (/).
This causes a NullReferenceException for any code that enumerates config.SectionGroups, including the pattern shown in Microsoft's own documentation example.
Reproduction Steps
Project file
run with
dotnet run.Expected behavior
Consistent info between SecionGroups.Count and non-null entries in the collection. The SectionGroups enumerator skips entries it cannot resolve rather than yielding null.
Actual behavior
Regression?
Unknown.
Known Workarounds
Add a null check when enumerating section groups:
Configuration
Other information
The root cause appears to be that
ConfigurationManageruses/as an internal path separator for section group hierarchies (e.g.,system.web.extensions/scripting/webServices). When a<sectionGroup>element has a name containing/, the internal lookup by path fails to resolve the group record, returningnull— but the count and enumerator still report it as present.