none
Custom configuration section - MSDN sample not working RRS feed

  • Question

  • Hi,

    I tried to copy & paste a MSDN sample for the creation of a custom configuration section into a test project for better understanding. Unfortunately, it is not working.

    First the link: http://msdn.microsoft.com/de-de/library/system.configuration.configurationproperty.aspx

    What I did was the following:

    1. Creating new class file and pasting this code taken from MSDN into it:

    Imports System
    Imports System.Configuration
    Imports System.Collections
    Imports System.ComponentModel
    
    ' Define a custom section.
    ' Shows how to use the ConfigurationProperty
    ' class when defining a custom section.
    Public NotInheritable Class CustomSection
      Inherits ConfigurationSection
    
      ' The collection (property bag) that contains 
      ' the section properties.
      Private Shared _Properties As ConfigurationPropertyCollection
    
      ' The FileName property.
      Private Shared _FileName As ConfigurationProperty
    
      ' The Alias property.
      Private Shared _Alias As ConfigurationProperty
    
      ' The MasUsers property.
      Private Shared _MaxUsers As ConfigurationProperty
    
      ' The MaxIdleTime property.
      Private Shared _MaxIdleTime As ConfigurationProperty
    
      ' CustomSection constructor.
      Shared Sub New()
    
        ' Initialize the _FileName property
        _FileName = New ConfigurationProperty( _
          "fileName", GetType(String), "default.txt")
    
        ' Initialize the _MaxUsers property
        _MaxUsers = New ConfigurationProperty( _
          "maxUsers", GetType(Long), 1000L, _
          ConfigurationPropertyOptions.None)
    
        ' Initialize the _MaxIdleTime property
        Dim minTime As TimeSpan = TimeSpan.FromSeconds(30)
        Dim maxTime As TimeSpan = TimeSpan.FromMinutes(5)
        Dim _TimeSpanValidator = _
          New TimeSpanValidator(minTime, maxTime, False)
    
        _MaxIdleTime = New ConfigurationProperty( _
          "maxIdleTime", GetType(TimeSpan), _
          TimeSpan.FromMinutes(5), _
          TypeDescriptor.GetConverter(GetType(TimeSpan)), _
          _TimeSpanValidator, _
          ConfigurationPropertyOptions.IsRequired, _
          "[Description:This is the max idle time.]")
    
        ' Initialize the _Alias property
        _Alias = New ConfigurationProperty( _
          "alias", GetType(String), "alias.txt")
    
        ' Property collection initialization.
        ' The collection (property bag) that contains 
        ' the properties is declared as:
        ' ConfigurationPropertyCollection _Properties;
        _Properties = New ConfigurationPropertyCollection()
        _Properties.Add(_FileName)
        _Properties.Add(_Alias)
        _Properties.Add(_MaxUsers)
        _Properties.Add(_MaxIdleTime)
    
      End Sub
    
      ' Return the initialized property bag
      ' for the configuration element.
      Protected Overrides ReadOnly Property Properties() _
      As ConfigurationPropertyCollection
        Get
          Return _Properties
        End Get
      End Property
    
      <StringValidator(InvalidCharacters:= _
      " ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
      MaxLength:=60)> _
      Public Property FileName() As String
        Get
          Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
          Me("fileName") = value
        End Set
      End Property
    
      <StringValidator(InvalidCharacters:= _
      " ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
      MaxLength:=60)> _
      Public Property [Alias]() As String
        Get
          Return CStr(Me("alias"))
        End Get
        Set(ByVal value As String)
          Me("alias") = value
        End Set
      End Property
    
      <LongValidator(MinValue:=1, _
      MaxValue:=1000000, ExcludeRange:=False)> _
      Public Property MaxUsers() As Long
        Get
          Return CLng(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
          Me("maxUsers") = value
        End Set
      End Property
    
      Public Property MaxIdleTime() As TimeSpan
        Get
          Return CType(Me("maxIdleTime"), TimeSpan)
        End Get
        Set(ByVal value As TimeSpan)
          Me("maxIdleTime") = value
        End Set
      End Property
    End Class
    
    
    

    2. Editing the app.config-file as follows:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<configSections>
    		<section name="CustomSection" type="Configuration.CustomSection, Configuration"
    		 allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
    		 restartOnExternalChanges="true" />
    	</configSections>
    	<CustomSection fileName="override.txt" alias="alias.txt"
    	 maxUsers="1000" maxIdleTime="00:05:00" />
    </configuration>
    

    Error message is: "Could not find schema information for the element 'CustomSection'."

    Here are my assembly properties:

    - AssemblyName: Configuration

    - RootNamespace: Configuration

    Should be obvious what's wrong, but I did not find it the last 30 minutes. Thanks for you help!

    Thursday, March 17, 2011 6:56 PM

Answers

  • Hi praskash_jda,

    thanks for your answer. The namespace declaration was not necessary. The code is working as is. Problem was that at the beginning I did a mistake when referencing from the app.config to the class definition and after that I tried to get rid of the warnings displayed when opening the app.config. These warnings are still there (a bug probably) but the configuration itself works.

    Cheers!

    • Marked as answer by Torsten Tiedt Friday, March 18, 2011 1:12 PM
    Friday, March 18, 2011 1:12 PM

All replies

  • I think you are missing Configuration namespace before your class declaration.

    Can you try something like this,

    Namespace Configuration
    
    Public NotInheritable Class CustomSection
     Inherits ConfigurationSection
    
    
    

     

    let me know if this doesn't work, I'll try creating a sample for you.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Friday, March 18, 2011 7:03 AM
  • Hi praskash_jda,

    thanks for your answer. The namespace declaration was not necessary. The code is working as is. Problem was that at the beginning I did a mistake when referencing from the app.config to the class definition and after that I tried to get rid of the warnings displayed when opening the app.config. These warnings are still there (a bug probably) but the configuration itself works.

    Cheers!

    • Marked as answer by Torsten Tiedt Friday, March 18, 2011 1:12 PM
    Friday, March 18, 2011 1:12 PM