Using an external .config file in configSource produces error

前端 未结 4 1163
迷失自我
迷失自我 2021-01-04 01:35

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 articl

相关标签:
4条回答
  • 2021-01-04 02:17

    If the section you are making external is defined in configSections, you should place the configSource attribute in the element defining the section. Only the appSettings and connectionStrings sections (which don't need definitions in configSections) should have tags with configSource in the body of the main config file.

    0 讨论(0)
  • 2021-01-04 02:18

    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!

    0 讨论(0)
  • 2021-01-04 02:22

    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 ??

    0 讨论(0)
  • 2021-01-04 02:33

    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" />
    
    0 讨论(0)
提交回复
热议问题