Question Custom Appconfig does not load.

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
I have some custom configs in my appconfig file but I get an error when I try loading int with the configurationManager getsection method. This is the message "Unrecognized element 'MiscFields'."
loading the setting:
VB.NET:
  _zollPcrSetting = System.Configuration.ConfigurationManager.GetSection("EnRoute/ZollPCRSettings")


This is the appconfig
HTML:
<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="EnRoute" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
       <section name ="ZollPCRSettings" type="EnRoute.Interface.Exchange.Vendor.Settings.ZollPCRSettings, EnRoute.Interface.Exchange, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
 </sectionGroup>
  </configSections>
 
  <EnRoute>
  
    <ZollPCRSettings UseTruckAgency="False" NatureCodeSize="0" ExcludeLocationName="False" OutputDirectory="C:\ZFiles" SunproActive="False">
      <MiscFields>
        <add FieldName="FirstResponder"/>
        <add FieldName="UnitStaged"/>
      </MiscFields>
    </ZollPCRSettings>
  </EnRoute>
  <system.diagnostics>
    <switches>
      <add name="holdForDebug" value="1"/>
    </switches>
  </system.diagnostics>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
</configuration>

VB.NET:
Imports System.Configuration


Namespace Vendor.Settings
    Public Class ZollPCRSettings
        Inherits ConfigurationSection

#Region "Static Variables"
        Private Shared _useTruckAgency As ConfigurationProperty
        Private Shared _natureCodeSize As ConfigurationProperty
        Private Shared _excludeLocationName As ConfigurationProperty
        Private Shared _outputDirectory As ConfigurationProperty
        Private Shared _sunproActive As ConfigurationProperty
        ' Private Shared _misc As ConfigurationProperty
        Private Shared _properties As ConfigurationPropertyCollection
#End Region

#Region "Properties"
        <ConfigurationProperty("UseTruckAgency", IsRequired:=False)> _
        Public Property UseTruckAgency() As Boolean
            Get
                Return Me(_useTruckAgency)
            End Get
            Set(ByVal Value As Boolean)
                Me(_useTruckAgency) = Value
            End Set
        End Property

        <ConfigurationProperty("NatureCodeSize", IsRequired:=False)> _
        Public Property NatureCodeSize() As Integer
            Get
                Return Me(_natureCodeSize)
            End Get
            Set(ByVal Value As Integer)
                Me(_natureCodeSize) = Value
            End Set
        End Property
        <ConfigurationProperty("ExcludeLocationName", IsRequired:=False)> _
        Public Property ExcludeLocationName() As Boolean
            Get
                Return Me(_excludeLocationName)
            End Get
            Set(ByVal Value As Boolean)
                Me(_excludeLocationName) = Value
            End Set
        End Property
        <ConfigurationProperty("OutputDirectory", IsRequired:=False)> _
        Public Property OutputDirectory() As String
            Get
                Return Me(_outputDirectory)
            End Get
            Set(ByVal Value As String)
                Me(_outputDirectory) = Value
            End Set
        End Property
        <ConfigurationProperty("SunproActive", IsRequired:=False)> _
        Public Property SunproActive() As Boolean
            Get
                Return Me(_sunproActive)
            End Get
            Set(ByVal Value As Boolean)
                Me(_sunproActive) = Value
            End Set
        End Property
        '<ConfigurationProperty("Misc", IsRequired:=False)> _
        'Public Property Misc() As IEnumerable(Of String)
        '    Get
        '        Return Me(_misc)
        '    End Get
        '    Set(ByVal Value As IEnumerable(Of String))
        '        Me(_misc) = Value
        '    End Set
        'End Property
        Protected Overrides ReadOnly Property Properties() As System.Configuration.ConfigurationPropertyCollection
            Get
                Return _properties
            End Get
        End Property
        <ConfigurationProperty("MiscFields")> _
        Public ReadOnly Property MiscFields() As MiscFieldCollection
            Get
                Dim collection As MiscFieldCollection = CType(Me("MiscFields"), MiscFieldCollection)
                Return collection
            End Get
        End Property
#End Region

#Region "Constructor"
        Shared Sub New()
            _useTruckAgency = New ConfigurationProperty("UseTruckAgency", GetType(Boolean), False, ConfigurationPropertyOptions.None)
            _natureCodeSize = New ConfigurationProperty("NatureCodeSize", GetType(Integer), 0, ConfigurationPropertyOptions.None)
            _excludeLocationName = New ConfigurationProperty("ExcludeLocationName", GetType(Boolean), False, ConfigurationPropertyOptions.None)
            _outputDirectory = New ConfigurationProperty("OutputDirectory", GetType(String), String.Empty, ConfigurationPropertyOptions.None)
            _sunproActive = New ConfigurationProperty("SunproActive", GetType(Boolean), False, ConfigurationPropertyOptions.None)
            '  _misc = New ConfigurationProperty("Misc", GetType(IEnumerable(Of String)), False, ConfigurationPropertyOptions.None)
            _properties = New ConfigurationPropertyCollection()
            _properties.Add(_useTruckAgency)
            _properties.Add(_natureCodeSize)
            _properties.Add(_excludeLocationName)
            _properties.Add(_outputDirectory)
            _properties.Add(_sunproActive)
            ' _properties.Add(_misc)

        End Sub
#End Region

    End Class
    Public Class MiscFieldCollection
        Inherits ConfigurationElementCollection

        Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
            Return New MiscField
        End Function

        Protected Overrides Function GetElementKey(element As ConfigurationElement) As Object
            Return DirectCast(element, MiscField).FieldName
        End Function
        Public ReadOnly Property i(ByVal idx As Integer) As MiscField
            Get
                Return CType(BaseGet(idx), MiscField)
            End Get

        End Property
    End Class

    Public Class MiscField
        Inherits System.Configuration.ConfigurationElement
        <ConfigurationProperty("FieldName", isRequired:=True)> _
        Public Property FieldName() As String
            Get
                Return Me("FieldName")
            End Get
            Set(value As String)
                Me("FieldName") = value
            End Set
        End Property
    End Class

End Namespace
 
I solved problem with the custom appconfig loading

I think that I removed the property "properties" which overrides the base property and now everything loads. There may really be an issue with the way I structured everything in the ZollPCRSetting class but by not overriding that property it loads now.

VB.NET:
       Protected Overrides ReadOnly Property Properties() As System.Configuration.ConfigurationPropertyCollection
            Get
                Return _properties
            End Get
        End Property
 

Latest posts

Back
Top