Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConfigurationManager.GetSection throws exception: "The entry '____' has already been added&quot

Status
Not open for further replies.

Shimmy613

Programmer
Feb 14, 2011
39
US
I’m using
System.Configuration.ConfigurationManager.GetSection​
to read a custom configuration section from an App.config. If I have two nodes that contain an identical value (such as the two “Station” nodes, below, with the identical highlighted values) – which is a project requirement – I get the error below when method GetSection is called.
The entry 'C:\Local_folder_path_goes_here' has already been added.​

Here’s the config file:

XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="StationsSection" type="TV_EcFtpClient.StationConfigurationSection, TV_EcFtpClient"/>
  </configSections>
  <StationsSection>
    <Stations>
      <add FtpHostname ="ftp://upload.acmeCompany.com/" DestinationFolderPath="[highlight #FCE94F]C:\Local_folder_path_goes_here[/highlight]" FtpFolderPath="acme_ftp/SourceFolder/" FtpUsername="fred" FtpPassword="fr3d$p@sswurd" FtpTimeoutInSeconds="30"/>
      <add FtpHostname ="ftp://upload.acmeCompany.com/" DestinationFolderPath="[highlight #FCE94F]C:\Local_folder_path_goes_here[/highlight]" FtpFolderPath="acme_ftp/SourceFolder/" FtpUsername="fred" FtpPassword="fr3d$p@sswurd" FtpTimeoutInSeconds="30"/>
    </Stations>
  </StationsSection>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <appSettings>
    <add key="NameOfService" value="ECClient"/>
    <add key="PollingFrequencyInSeconds" value="60"/>
  </appSettings>
</configuration>

I’ve seen this issue all over Google, but nothing that’s helped me solve the issue yet.

Thanks,
Eliezer
 
I found the problem. I had the method

Code:
protected override object GetElementKey(ConfigurationElement element)

returning the "DestinationFolderPath", so it was expected to be unique. Instead, I found a property that would actually be unique.

And if there wouldn't have been one property that would always be unique, I would have done a "composite key" approach to return as the key. For example:
[tt]return station.FtpHostname + station.DestinationFolderPath + station.FtpFolderPath + station.FtpUsername;[/tt]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top