Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to understand System.Configuration. I have been trying to use some examples of creating custom configuration sections in a VB.NET app. Not having much luck, so any help would be appreciated.

Thanks,
ed

Posted is the error message I get:

System.Configuration.ConfigurationErrorsException was unhandled
BareMessage="An error occurred creating the configuration section handler for DeleteGroup/DeleteSection: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"
Filename="C:\Devel_NET\Test_Custom_App_Config\Test_Custom_App_Config\bin\Debug\Test_Custom_App_Config.vshost.exe.Config"
Line=7
Message="An error occurred creating the configuration section handler for DeleteGroup/DeleteSection: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) (C:\Devel_NET\Test_Custom_App_Config\Test_Custom_App_Config\bin\Debug\Test_Custom_App_Config.vshost.exe.Config line 7)"
Source="System.Configuration"
StackTrace:
at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
at

The following is the code...

MAIN:

XML
Imports System.Text

Module Module1

    Sub Main()
        Dim config As New Handler
        config = CType(System.Configuration.ConfigurationManager.GetSection("DeleteGroup/DeleteSection"), Handler)
        Dim sb As New StringBuilder
        sb.Append("<h2>Attributes in the DeleteSection Element:</h2>")
        sb.AppendFormat("Attrib1 = {0}<br/>", config.Attrib1.ToString())
        sb.Append("<h2>Attributes in the Delete_Flatness Section Element:</h2>")
        sb.AppendFormat("DaysOld = {0}<br/>", config.DaysOld.ToString())
        sb.AppendFormat("SearchDir = {0}<br/>", config.SearchDir.ToString())
        sb.AppendFormat("SearchString = {0}<br/>", config.SearchString.ToString())

        Console.WriteLine(sb.ToString())
    End Sub
End Module


CONFIG HANDLER CLASS:

VB
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml


Public Class Handler

    Inherits ConfigurationSection

    Public Sub Handler()
    End Sub

    Public Sub Handler(ByVal attribVal As String)
        Attrib1 = attribVal
    End Sub

    <ConfigurationProperty("Attrib1")> _
    Public Property Attrib1() As String
        Get
            Return CStr(Me("Attrib1"))
        End Get
        Set(ByVal value As String)
            Me("Attrib1") = value
        End Set
    End Property

    <ConfigurationProperty("SearchDir")> _
    Public Property SearchDir() As String
        Get
            Return CStr(Me("SearchDir"))
        End Get
        Set(ByVal value As String)
            Me("SearchDir") = value
        End Set
    End Property
    <ConfigurationProperty("SearchString")> _
    Public Property SearchString() As String
        Get
            Return CStr(Me("SearchString"))
        End Get
        Set(ByVal value As String)
            Me("SearchString") = value
        End Set
    End Property
    <ConfigurationProperty("DaysOld")> _
    Public Property DaysOld() As Integer
        Get
            Return CInt(Me("DaysOld"))
        End Get
        Set(ByVal value As Integer)
            Me("DaysOld") = value
        End Set
    End Property

End Class


APP.CONFIG ENTRIES:

XML
<!-- Configuration section-handler declaration area. -->
    <configSections>
        <sectionGroup name="DeleteGroup">
            <section
              name="DeleteSection"
              type="Handler, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
              allowLocation="true"
              allowDefinition="Everywhere"/>
        </sectionGroup>
        <!-- Other <section> and <sectionGroup> elements. -->
    </configSections>



XML
<!-- Configuration section settings area. -->
    <DeleteGroup>
        <DeleteSection Attrib1="Flatness">
            <Delete_Flatness
                SearchDir="C:\N2CM_Flatdc"
                SearchString="*.dat"
                DaysOld="120"/>
        </DeleteSection>
    </DeleteGroup>
Posted
Updated 24-Feb-10 13:37pm
v2

1 solution

You are missing the Namespace in the type attribute of your section in App.Config. Infact I don't believe you need the full assembly info in there either.

Check out Understanding Section Handlers - App.config File[^] for a good explanation of how this works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900