Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an external .config file in configSource produces error

I was playing around with how to use the Configuration Manager to read/write custom sections in the App.config file for a WPF application in C#. I read this excellent article on .NET 2.0 Configuration Demystified and it helped me a lot in using the config files. Here is the initial App.config file which I wrote and it works fine.

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 in such a way that my custom section will be read from an external config file mentioned in configSource, Visual Studio gives me an error

The format of a 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>
like image 548
chaitanya Avatar asked May 12 '11 15:05

chaitanya


People also ask

Can DLL have config file?

Using configuration files in DLL is not trivial, but is very simple. Using configuration files in DLL is not trivial. To accomplish this, we must follow these steps: Add a reference to System.

Where do I put appSettings in web config?

Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.

What is appSettings section in web config file?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.


3 Answers

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

if you need conserve some project related keys in web.config and your personalized key in another file (recommended for security reasons), use 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 it help others!

like image 93
equiman Avatar answered Sep 30 '22 01:09

equiman


Visual Studio's editor/intellisense has a shortcoming in that it complains about the configSource= attribute - but it is absolutely legal, and it does work; I use it every day, in various production systems.

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

Update: OK - well it seems you're totally changing the style of the <example> tag. In your original app.config you have:

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

So of course, your externalized example.config must 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 ??

like image 30
marc_s Avatar answered Sep 30 '22 02:09

marc_s


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

Incorrect:

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

If you delete the "add" tag or move it into your configSource file, the error goes away.

Correct:

<appSettings configSource="Appsettings.config" />
like image 25
Brett Pennings Avatar answered Sep 30 '22 02:09

Brett Pennings