Using an external .config file in configSource causes an error

I played with how to use Configuration Manager to read / write custom sections in the App.config file for a WPF application in C #. I read this great article on the .NET 2.0 Configuration Demystified , and it really helped me in using the configuration files. Here is the initial App.config file I wrote, and it works great.

App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="example" type="CustomConfig.ExampleSection, CustomConfig" /> </configSections> <example version="A sample string value." /> <appSettings> <add key="version_string" value="1.01" /> </appSettings> </configuration> 

But when I changed the App.config file so that my user section will be read from the external configuration file mentioned in configSource, Visual Studio gives me an error

The format of the configSource file must be an element containing the name of the section.

Here are the App.config and example.config files

Changed App.config

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="example" type="CustomConfig.ExampleSection, CustomConfig" /> </configSections> <example configSource="example.config" /> <appSettings> <add key="version_string" value="1.01" /> </appSettings> </configuration> 

example.config

 <?xml version="1.0"?> <example> <add key="version" value="blahblah" /> </example> 
+6
source share
4 answers

Did you actually try to run the code?

The Visual Studio / intellisense editor has the disadvantage that it complains about the configSource= attribute, but it IS absolutely legal, but it works ! I use it every day, in various production systems.

My recommendation: give it a try! Run the code - I'm sure it will work (your configs look good to me).

Update: OK - it looks like you are completely changing the style of the <example> . In the original app.config you have:

 <example version="A sample string value." /> 

So, your externalized example.config should contain the same values ​​and the same structure:

 <?xml version="1.0"?> <example version="A sample string value." /> 

Can you try with this example.config ??

+2
source

I got the same error. In my case, due to the fact that I have keys in two files, then detect the appSettings tag as duplicated.

if you need to save some project-related keys in web.config and your personalized key in another file (recommended for security reasons), use the file property instead of configSource .

web.config file :

 <configuration> <appSettings file="../AppSettings.config"> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> </appSettings> </configuration> 

AppSettings.config File :

 <?xml version="1.0"?> <appSettings> <add key="RutaBodega" value="D:\Test\Card"/> <add key="CodeLen" value="5"/> </appSettings> 

Hope this helps others!

+4
source

My problem is that I had adding configSource AND key in the same tag.

Wrong:

 <appSettings configSource="Appsettings.config"> <add key="Setting1" value="May 5, 2014"/> </appSettings> 

If you remove the "add" tag or move it to your configSource file, the error will disappear.

Right:

 <appSettings configSource="Appsettings.config" /> 
+3
source

If the section that you make external is defined in configSections, you must put the configSource attribute in the element that defines the section. Only appSettings and connectionStrings sections (which do not need definitions in configSections) should have tags with configSource in the body of the main configuration file.

+1
source

Source: https://habr.com/ru/post/887943/


All Articles