Ask a questionAsk a question
 

AnswerCustomValiadtor in CustomConfigSection

  • Thursday, November 05, 2009 1:47 PMHarry14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    I wrote a custom ConfigSection like this:
    Public Class LoaderConfigSection
            Inherits AConfigSectionBase
    
            Private Shared _section As LoaderConfigSection
    
            Private Shared _instantiationLock As New ReaderWriterLockSlim
    
            Private Sub New()
    
            End Sub
    
            Public Shared Function getSection() As LoaderConfigSection
                Try
                    _instantiationLock.EnterWriteLock()
                    If _section Is Nothing Then
                        _section = CType(_config.GetSection("frameworkConfigGroup/loader"), LoaderConfigSection)
    
                        If _section Is Nothing Then
                            Throw New ConfigurationErrorsException("Section not found!!")
                        End If
                    End If
    
                    Return _section
                Catch ex As Exception
                    Throw
                Finally
                    _instantiationLock.ExitWriteLock()
                End Try
            End Function
    
            ' <ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True), StringValueValidator("singleAppDomain;multipleAppdomains")> _
    
            '<ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True)> _
            <ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True), StringValueValidator("singleAppDomain;multipleAppdomains")> _
            Public Property Mode() As String
    
                Get
                    Try
                        _lock.EnterReadLock()
                        Return (CType(Me("Mode"), String))
                    Catch ex As Exception
                        Throw
                    Finally
                        _lock.ExitReadLock()
                    End Try
                End Get
                Set(ByVal value As String)
                    Try
                        _lock.EnterWriteLock()
                        Me("Mode") = value
                    Catch ex As Exception
                        Throw
                    Finally
                        _lock.ExitWriteLock()
                    End Try
                End Set
            End Property
    
        End Class
    End Namespace
    
    Within the section I try to use a custom validator object the StringValueValidator that looks like this:
    <AttributeUsage(AttributeTargets.All)> _
        Public Class StringValueValidatorAttribute
            Inherits ConfigurationValidatorAttribute
            Private _validator As StringValueValidator
    
            Public Property ValidStrings() As String
                Get
                    Return _validator.ValidStrings
                End Get
                Set(ByVal value As String)
                    _validator.ValidStrings = value
                End Set
            End Property
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="validStrings">the valid strings seperated by ;</param>
            ''' <remarks></remarks>
            Public Sub New(ByVal validStrings As String)
                _validator = New StringValueValidator(validStrings)
            End Sub
    
            Public Function CanValidate(ByVal type As System.Type) As Boolean
                Return _validator.CanValidate(type)
            End Function
    
            Public Sub Validate(ByVal value As Object)
                _validator.Validate(value)
            End Sub
    
        End Class
    
    But everytime I start the app, an exception occurs: Error when creating the ConfigurationSectionHandler.The value must not be NULL. Parametername:Type.

    This exception only occurs, when I use the Stringvaluevalidator.

    Thank you.

    Harry

Answers

  • Thursday, November 05, 2009 3:25 PMTaylorMichaelLMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Your string validator class must derive from ConfigurationValidatorBase.  The CanValidate and Validate methods must be marked as overridable.  You must also implement the ValidatorInstance property on your validator attribute.  This property can return your created validator instance.

    Public Class StringValueValidatorAttribute
       ...
    
       Public Overrides ReadOnly Property ValidatorInstance As ConfigurationValidatorBase
          Get
             Return _validator
          End Get
       End Property
    End Class
    
    Public Class StringValueValidator
       Inherits ConfigurationValidatorBase
     
       ...
    End Class
    
    Michael Taylor - 11/5/09
    http://p3net.mvps.org
    • Marked As Answer byHarry14 Friday, November 06, 2009 8:13 AM
    • Unmarked As Answer byHarry14 Friday, November 06, 2009 8:18 AM
    • Marked As Answer byHarry14 Friday, November 06, 2009 8:19 AM
    •  

All Replies

  • Thursday, November 05, 2009 3:25 PMTaylorMichaelLMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Your string validator class must derive from ConfigurationValidatorBase.  The CanValidate and Validate methods must be marked as overridable.  You must also implement the ValidatorInstance property on your validator attribute.  This property can return your created validator instance.

    Public Class StringValueValidatorAttribute
       ...
    
       Public Overrides ReadOnly Property ValidatorInstance As ConfigurationValidatorBase
          Get
             Return _validator
          End Get
       End Property
    End Class
    
    Public Class StringValueValidator
       Inherits ConfigurationValidatorBase
     
       ...
    End Class
    
    Michael Taylor - 11/5/09
    http://p3net.mvps.org
    • Marked As Answer byHarry14 Friday, November 06, 2009 8:13 AM
    • Unmarked As Answer byHarry14 Friday, November 06, 2009 8:18 AM
    • Marked As Answer byHarry14 Friday, November 06, 2009 8:19 AM
    •  
  • Friday, November 06, 2009 8:19 AMHarry14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks a lot that worked.
  • Monday, November 09, 2009 3:38 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you, Michael, for the sharing of the solution! :)
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.