none
System.Configuration: The entry '' has already been added.

    Question

  • Hello,

    This is my first thread on MSDN forum. I have an issue with custom configuration. My custom application configuration looks like this:

    1 <?xml version="1.0" encoding="utf-8" ?> 
    2 <configuration> 
    3   <configSections> 
    4     <section name="ServerConfiguration.ServerConfig" type="ServerConfiguration.ServerConfig, ServerConfiguration"/>  
    5   </configSections> 
    6   <ServerConfiguration.ServerConfig> 
    7     <Servers> 
    8       <clearServers/> 
    9       <Server name="localhost1" port="8080" protocol="net.tcp" hostname="localhost"/>  
    10       <Server name="localhost2" port="8081" protocol="net.tcp" hostname="localhost"/>  
    11     </Servers> 
    12     <Regions> 
    13       <clearRegions/> 
    14       <removeRegion name=""/>  
    15       <Region name="BASE" connectionString="XXXXXXXXXX" servicePath="D:\Services" clientPath="D:\">  
    16         <Services> 
    17           <clearServices/> 
    18           <Service name="XXXXX"/>  
    19         </Services> 
    20       </Region> 
    21     </Regions> 
    22   </ServerConfiguration.ServerConfig> 
    23 </configuration> 

    (Please Note: The above configuration schema is changed. Please find the new configuration schema somewhere in following replies. Thanks, - Jay)
    The "Services" ConfigurationElementCollection has been nested in "Region" ConfigurationElement.
    When I try to parse this configuration section, I get following error message:



    1 System.Configuration.ConfigurationErrorsException was unhandled     
    2   BareMessage="The entry '' has already been added."    
    3   Filename=""    
    4   Line=0     
    5   Message="The entry '' has already been added."    
    6   Source="System.Configuration"    
    7   StackTrace:     
    8        at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)    at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)    at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)    at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)    at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)    at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)    at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)    at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)    at System.Configuration.ConfigurationManager.GetSection(String sectionName)    at TestApp.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\mmmm\My Documents\Visual Studio 2008\Projects\Server\TestApp\Form1.vb:line 6    at System.Windows.Forms.Control.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ButtonBase.WndProc(Message& m)    at System.Windows.Forms.Button.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(Form mainForm)    at TestApp.Form1.Main() in C:\Documents and Settings\mmmm\My Documents\Visual Studio 2008\Projects\Server\TestApp\Form1.Designer.vb:line 2    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()     
    9   InnerException:      
    10     
    11  




    If I remove nesting of "Services" ConfigurationElementCollection from "Region" ConfigurationElement then it works!!! What is the problem?

    Please help!

    Thanks,
    Jay
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:42 PM
    Tuesday, October 21, 2008 3:42 PM

Answers

  • Although code such as the following appears in the documentation samples, it does not seem correct.  I do not understand why one would want to add a blank item to the collection as soon as the collection is constructed.  It seems to me that all items should be explicitly added in the configuration file.

    I found a blog post (http://blogs.neudesic.com/blogs/jason_jung/archive/2006/08/08/208.aspx) that does not use this kind of constructor code.

    By commenting out as follows, it seems to work:

        Public Sub New()  
            'Dim serverElement As ServerElement = _  
            'CType(CreateNewElement(), ServerElement)  
            'Add(serverElement)  
        End Sub 'New    

        Public Sub New()  
            'Dim regionElement As RegionElement = _  
            'CType(CreateNewElement(), RegionElement)  
            'Add(regionElement)  
        End Sub 'New    

        Public Sub New()  
            'Dim serviceElement As ServiceElement = _  
            'CType(CreateNewElement(), ServiceElement)  
            'Add(serviceElement)  
        End Sub 'New    


    • Marked as answer by Zhi-Xin Ye Monday, October 27, 2008 11:31 AM
    Friday, October 24, 2008 1:37 AM

All replies

  • It would be very helpful if you could post code for the ServerConfiguration.ServerConfig.ServerConfiguration class.

    Wednesday, October 22, 2008 2:03 AM
  • Here is the code:

    (Part-1)

    #Region " Namespace Imports "  
     
    Imports System.Configuration
    #End Region  
     
    #Region " ServerConfig " 
     
    Public NotInheritable Class ServerConfig  
        Inherits ConfigurationSection  
     
        <ConfigurationProperty("Servers", IsDefaultCollection:=True, IsRequired:=True), ConfigurationCollection(GetType(ServerCollection), AddItemName:="Server", ClearItemsName:="clearServers", RemoveItemName:="removeServer")> _  
        Public ReadOnly Property Servers() As ServerCollection  
            Get 
                Dim serverCollection As ServerCollection = CType(MyBase.Item("Servers"), ServerCollection)  
                Return serverCollection  
            End Get 
        End Property 
    End Class
    #End Region  
     
    #Region " Servers " 
     
    Public NotInheritable Class ServerCollection  
        Inherits ConfigurationElementCollection  
     
        Public Sub New()  
            Dim serverElement As ServerElement = _  
            CType(CreateNewElement(), ServerElement)  
            Add(serverElement)  
        End Sub 'New  
     
        Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType  
            Get 
                Return ConfigurationElementCollectionType.AddRemoveClearMap  
            End Get 
        End Property 
     
        Protected Overrides Function CreateNewElement() As ConfigurationElement  
            Return New ServerElement()  
        End Function 
     
        Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As [Object]  
            Return CType(element, ServerElement).Name  
        End Function 'GetElementKey  
     
        Default Public Shadows Property Item(ByVal index As IntegerAs ServerElement  
            Get 
                Return CType(BaseGet(index), ServerElement)  
            End Get 
            Set(ByVal value As ServerElement)  
                If Not (BaseGet(index) Is NothingThen 
                    BaseRemoveAt(index)  
                End If 
                BaseAdd(index, value)  
            End Set 
        End Property 
     
        Default Public Shadows ReadOnly Property Item(ByVal Name As StringAs ServerElement  
            Get 
                Return CType(BaseGet(Name), ServerElement)  
            End Get 
        End Property 
     
        Public Function IndexOf(ByVal server As ServerElement) As Integer 
            Return BaseIndexOf(server)  
        End Function 
     
        Public Sub Add(ByVal server As ServerElement)  
            BaseAdd(server)  
        End Sub 
     
        Protected Overrides Sub BaseAdd(ByVal element As ConfigurationElement)  
            BaseAdd(element, False)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal server As ServerElement)  
            If BaseIndexOf(server) >= 0 Then 
                BaseRemove(server.Name)  
            End If 
        End Sub 
     
        Public Sub RemoveAt(ByVal index As Integer)  
            BaseRemoveAt(index)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal name As String)  
            BaseRemove(name)  
        End Sub 
     
        Public Sub Clear()  
            BaseClear()  
        End Sub 
    End Class 
     
    Public NotInheritable Class ServerElement  
        Inherits ConfigurationElement  
     
        Public Sub New(ByVal name As StringByVal hostName As StringByVal port As IntegerByVal protocol As String)  
            Me.Name = name  
            Me.Port = port  
            Me.Protocol = protocol  
            Me.HostName = hostName  
        End Sub 'New  
     
        Public Sub New()  
        End Sub 
     
        <ConfigurationProperty("name", IsRequired:=True, IsKey:=True)> _  
        Public Property Name() As String 
            Get 
                Return CStr(Me("name"))  
            End Get 
            Set(ByVal value As String)  
                Me("name") = value  
            End Set 
        End Property 
     
        '<ConfigurationProperty("port", IsRequired:=False), IntegerValidator(MinValue:=0, MaxValue:=65000, ExcludeRange:=False)> _  
        <ConfigurationProperty("port", IsRequired:=False)> _  
    Public Property Port() As String 
            Get 
                Return CStr(Me("port"))  
            End Get 
            Set(ByVal value As String)  
                Me("port") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("protocol", IsRequired:=True)> _  
        Public Property Protocol() As String 
            Get 
                Return CStr(Me("protocol"))  
            End Get 
            Set(ByVal value As String)  
                Me("protocol") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("hostname", IsRequired:=True)> _  
        Public Property HostName() As String 
            Get 
                Return CStr(Me("hostname"))  
            End Get 
            Set(ByVal value As String)  
                Me("hostname") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("Regions", IsDefaultCollection:=False, IsRequired:=True), ConfigurationCollection(GetType(RegionCollection), AddItemName:="Region", ClearItemsName:="clearRegions", RemoveItemName:="removeRegion")> _  
        Public ReadOnly Property Regions() As RegionCollection  
            Get 
                Dim regionCollection As RegionCollection = CType(MyBase.Item("Regions"), RegionCollection)  
                Return regionCollection  
            End Get 
        End Property 
    End Class
    #End Region 
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:25 PM
    Wednesday, October 22, 2008 2:12 PM

  • Code (Part-2):
     
    #Region " Regions "  
     
    Public NotInheritable Class RegionCollection  
        Inherits ConfigurationElementCollection  
     
        Public Sub New()  
            Dim regionElement As RegionElement = _  
            CType(CreateNewElement(), RegionElement)  
            Add(regionElement)  
        End Sub 'New  
     
        Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType  
            Get 
                Return ConfigurationElementCollectionType.AddRemoveClearMap  
            End Get 
        End Property 
     
        Protected Overrides Function CreateNewElement() As ConfigurationElement  
            Return New RegionElement()  
        End Function 
     
        Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As [Object]  
            Return CType(element, RegionElement).Name  
        End Function 'GetElementKey  
     
        Default Public Shadows Property Item(ByVal index As IntegerAs RegionElement  
            Get 
                Return CType(BaseGet(index), RegionElement)  
            End Get 
            Set(ByVal value As RegionElement)  
                If Not (BaseGet(index) Is NothingThen 
                    BaseRemoveAt(index)  
                End If 
                BaseAdd(index, value)  
            End Set 
        End Property 
     
        Default Public Shadows ReadOnly Property Item(ByVal Name As StringAs RegionElement  
            Get 
                Return CType(BaseGet(Name), RegionElement)  
            End Get 
        End Property 
     
        Public Function IndexOf(ByVal region As RegionElement) As Integer 
            Return BaseIndexOf(region)  
        End Function 
     
        Public Sub Add(ByVal region As RegionElement)  
            BaseAdd(region)  
        End Sub 
     
        Protected Overrides Sub BaseAdd(ByVal element As ConfigurationElement)  
            BaseAdd(element, False)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal region As RegionElement)  
            If BaseIndexOf(region) >= 0 Then 
                BaseRemove(region.Name)  
            End If 
        End Sub 
     
        Public Sub RemoveAt(ByVal index As Integer)  
            BaseRemoveAt(index)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal name As String)  
            BaseRemove(name)  
        End Sub 
     
        Public Sub Clear()  
            BaseClear()  
        End Sub 
    End Class 
     
    Public NotInheritable Class RegionElement  
        Inherits ConfigurationElement  
     
        Public Sub New()  
        End Sub 
     
        Public Sub New(ByVal name As StringByVal connectionString As StringByVal servicePath As StringByVal clientPath As StringByVal services As ServiceCollection)  
            Me.Name = name  
            Me.ConnectionString = connectionString  
            Me.ServicePath = servicePath  
            Me.ClientPath = clientPath  
            'Me.Services = services  
        End Sub 'New  
     
        <ConfigurationProperty("name", IsRequired:=True, IsKey:=True)> _  
        Public Property Name() As String 
            Get 
                Return CStr(Me("name"))  
            End Get 
            Set(ByVal value As String)  
                Me("name") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("connectionString", IsRequired:=True)> _  
       Public Property ConnectionString() As String 
            Get 
                Return CStr(Me("connectionString"))  
            End Get 
            Set(ByVal value As String)  
                Me("connectionString") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("servicePath", IsRequired:=True)> _  
        Public Property ServicePath() As String 
            Get 
                Return CStr(Me("servicePath"))  
            End Get 
            Set(ByVal value As String)  
                Me("servicePath") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("clientPath", IsRequired:=True)> _  
        Public Property ClientPath() As String 
            Get 
                Return CStr(Me("clientPath"))  
            End Get 
            Set(ByVal value As String)  
                Me("clientPath") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("Services", IsDefaultCollection:=False, IsRequired:=True), ConfigurationCollection(GetType(ServiceCollection), AddItemName:="Service", ClearItemsName:="clearServices", RemoveItemName:="removeService")> _  
        Public ReadOnly Property Services() As ServiceCollection  
            Get 
                Dim serviceCollection As ServiceCollection = CType(MyBase.Item("Services"), ServiceCollection)  
                Return serviceCollection  
            End Get 
        End Property 
    End Class
    #End Region 
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:28 PM
    Wednesday, October 22, 2008 2:13 PM
  • Code (Part-3)

    #Region " Services "  
     
    Public NotInheritable Class ServiceCollection  
        Inherits ConfigurationElementCollection  
     
        Public Sub New()  
            Dim serviceElement As ServiceElement = _  
            CType(CreateNewElement(), ServiceElement)  
            Add(serviceElement)  
        End Sub 'New  
     
        Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType  
            Get 
                Return ConfigurationElementCollectionType.AddRemoveClearMap  
            End Get 
        End Property 
     
        Protected Overrides Function CreateNewElement() As ConfigurationElement  
            Return New ServiceElement()  
        End Function 
     
        Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As [Object]  
            Return CType(element, ServiceElement).Name  
        End Function 'GetElementKey  
     
        Default Public Shadows Property Item(ByVal index As IntegerAs ServiceElement  
            Get 
                Return CType(BaseGet(index), ServiceElement)  
            End Get 
            Set(ByVal value As ServiceElement)  
                If Not (BaseGet(index) Is NothingThen 
                    BaseRemoveAt(index)  
                End If 
                BaseAdd(index, value)  
            End Set 
        End Property 
     
        Default Public Shadows ReadOnly Property Item(ByVal Name As StringAs ServiceElement  
            Get 
                Return CType(BaseGet(Name), ServiceElement)  
            End Get 
        End Property 
     
        Public Function IndexOf(ByVal service As ServiceElement) As Integer 
            Return BaseIndexOf(service)  
        End Function 
     
        Public Sub Add(ByVal service As ServiceElement)  
            BaseAdd(service)  
        End Sub 
     
        Protected Overrides Sub BaseAdd(ByVal element As ConfigurationElement)  
            BaseAdd(element, False)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal service As ServiceElement)  
            If BaseIndexOf(service) >= 0 Then 
                BaseRemove(service.Name)  
            End If 
        End Sub 
     
        Public Sub RemoveAt(ByVal index As Integer)  
            BaseRemoveAt(index)  
        End Sub 
     
        Public Overloads Sub Remove(ByVal name As String)  
            BaseRemove(name)  
        End Sub 
     
        Public Sub Clear()  
            BaseClear()  
        End Sub 
    End Class 
     
    Public NotInheritable Class ServiceElement  
        Inherits ConfigurationElement  
     
        Public Sub New(ByVal name As String)  
            Me.Name = name  
        End Sub 'New  
     
        Public Sub New()  
        End Sub 
     
        <ConfigurationProperty("name", IsRequired:=True, IsKey:=True)> _  
        Public Property Name() As String 
            Get 
                Return CStr(Me("name"))  
            End Get 
            Set(ByVal value As String)  
                Me("name") = value  
            End Set 
        End Property 
     
        <ConfigurationProperty("dllName", IsRequired:=True)> _  
        Public Property DllName() As String 
            Get 
                Return CStr(Me("dllName"))  
            End Get 
            Set(ByVal value As String)  
                Me("dllName") = value  
            End Set 
        End Property 
    End Class
    #End Region 
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:29 PM
    Wednesday, October 22, 2008 2:14 PM
  • New Configuration File:

    1 <configSections> 
    2 <section name="ServerConfiguration.ServerConfig" type="ServerConfiguration.ServerConfig, ServerConfiguration"/>  
    3 </configSections> 
    4 <ServerConfiguration.ServerConfig> 
    5 <Servers> 
    6 <clearServers/> 
    7   <Server name="localhost1" port="8080" protocol="net.tcp" hostname="localhost">  
    8     <Regions> 
    9       <clearRegions/> 
    10       <removeRegion name=""/>  
    11       <Region name="BASE" connectionString="XXXXXXXXXX" servicePath="D:\Services" clientPath="D:\">  
    12         <Services> 
    13           <clearServices/> 
    14           <Service name="XXXXX" dllName="ABC"/>  
    15         </Services> 
    16       </Region> 
    17     </Regions> 
    18   </Server> 
    19 </Servers> 
    20 </ServerConfiguration.ServerConfig> 

    Thanks,
    Jay
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:51 PM
    Wednesday, October 22, 2008 2:19 PM
  •  To Parse configuration insert PropertyGrid control on form and use following code:

    Dim objServerConfig As ServerConfig  
    objServerConfig = ConfigurationManager.GetSection(configSectionName)  
    PropertyGrid1.SelectedObject = objServerConfig  
    Wednesday, October 22, 2008 2:36 PM
  • THE STRANGE THING:

                Dim objServerConfig As ServerConfig = Nothing 
                Try 
                    objServerConfig = ConfigurationManager.GetSection(configSectionName)  
                Catch ex As Exception  
                    objServerConfig = ConfigurationManager.GetSection(configSectionName)  
                End Try 
                PropertyGrid1.SelectedObject = objServerConfig 

    This code works!!! :((
    • Edited by Jaydeep Kher Wednesday, October 22, 2008 2:47 PM
    Wednesday, October 22, 2008 2:45 PM
  • Although code such as the following appears in the documentation samples, it does not seem correct.  I do not understand why one would want to add a blank item to the collection as soon as the collection is constructed.  It seems to me that all items should be explicitly added in the configuration file.

    I found a blog post (http://blogs.neudesic.com/blogs/jason_jung/archive/2006/08/08/208.aspx) that does not use this kind of constructor code.

    By commenting out as follows, it seems to work:

        Public Sub New()  
            'Dim serverElement As ServerElement = _  
            'CType(CreateNewElement(), ServerElement)  
            'Add(serverElement)  
        End Sub 'New    

        Public Sub New()  
            'Dim regionElement As RegionElement = _  
            'CType(CreateNewElement(), RegionElement)  
            'Add(regionElement)  
        End Sub 'New    

        Public Sub New()  
            'Dim serviceElement As ServiceElement = _  
            'CType(CreateNewElement(), ServiceElement)  
            'Add(serviceElement)  
        End Sub 'New    


    • Marked as answer by Zhi-Xin Ye Monday, October 27, 2008 11:31 AM
    Friday, October 24, 2008 1:37 AM
  • Hello BinaryCoder,

    Thank you very much for your help. I got that code from some blog and I didn't looked at constructors in Element classes.

    Thanks Again!
    Have a nice day!

    Jay
    Friday, October 24, 2008 2:26 PM
  • Me too

    InstallSection _section;
    try
    {
        _section= (InstallSection)ConfigurationManager.GetSection("sectionName");
    }
    catch
    {
        _section= (InstallSection)ConfigurationManager.GetSection("sectionName");
    }


    And works
    Monday, September 09, 2013 9:55 PM