Description
I ran into a problem with the current implementation of Microsoft.Extensions.Configuration.Xml.
With Visual Studio 2019 16.2.1 I created a new Console App (.NET Framework) application,
added NuGet packages Microsoft.Extensions.Configuration.Xml and NLog.Extensions.Logging,
added code var configuration = new ConfigurationBuilder().AddXmlFile(Assembly.GetExecutingAssembly().Location + ".config").Build();
into main function,
started the program and got an exception:
System.FormatException
HResult=0x80131537
Message=XML namespaces are not supported. Line 7, position 22.
Source=Microsoft.Extensions.Configuration.FileExtensions
StackTrace:
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at ConsoleApp1.Program.Main(String[] args)
I looked into the generated app.config file and found following XML data:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
The reason of FormatException is attribute "xmlns" of element "assemblyBinding".
I wonder, why files with XML namespace declarations are completely rejected.
My expectation would be, that XML features, that are not yet implemented or cannot be implemented due to their nature of not being mappable to key-value-pairs,
are ignored, perhaps writing a warning into log file and all other elements are loaded into memory.
So does it make sense to change the behaviour, instead of throwing an exception ignoring such elements and write a warning into log file?