Static Property losing its value intermittently ?

Posted by joedotnot on Stack Overflow See other posts from Stack Overflow or by joedotnot
Published on 2010-05-18T06:14:45Z Indexed on 2010/05/18 6:20 UTC
Read the original article Hit count: 269

Is there something fundamentally wrong with the following design, or can anyone see why would the static properties sometimes loose their values ?

I have a class library project containing a class AppConfig; this class is consumed by a Webforms project.

The skeleton of AppConfig class is as follows:

Public Class AppConfig
    Implements IConfigurationSectionHandler

    Private Const C_KEY1        As String = "WebConfig.Key.1"
    Private Const C_KEY2        As String = "WebConfig.Key.2"
    Private Const C_KEY1_DEFAULT_VALUE  as string = "Key1defaultVal"
    Private Const C_KEY2_DEFAULT_VALUE  as string = "Key2defaultVal"

    Private Shared m_field1 As String
    Private Shared m_field2 As String

    Public Shared ReadOnly Property ConfigValue1() As String
        Get
            ConfigValue1= m_field1
        End Get
    End Property

    Public Shared ReadOnly Property ConfigValue2() As String
        Get
            ConfigValue2 = m_field2
        End Get
    End Property


    Public Shared Sub OnApplicationStart()
        m_field1    = ReadSetting(C_KEY1, C_KEY1_DEFAULT_VALUE)
        m_field2    = ReadSetting(C_KEY2, C_KEY1_DEFAULT_VALUE)
    End Sub

    Public Overloads Shared Function ReadSetting(ByVal key As String, ByVal defaultValue As String) As String
        Try
            Dim setting As String = System.Configuration.ConfigurationManager.AppSettings(key)
            If setting Is Nothing Then
                ReadSetting = defaultValue
            Else
                ReadSetting = setting
            End If
        Catch
            ReadSetting = defaultValue
        End Try
    End Function

    Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
        Dim objSettings As NameValueCollection
        Dim objHandler As NameValueSectionHandler

        objHandler = New NameValueSectionHandler
        objSettings = CType(objHandler.Create(parent, configContext, section), NameValueCollection)

        Return 1
    End Function

End Class

The Static Properties get set once on application start, from the Application_Start event of the Global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    //Fires when the application is started
    AppConfig.OnApplicationStart()

End Sub

Thereafter, whenever we want to access a value in the Web.Config from anywhere, e.g. aspx page code-behind or another class or referenced class, we simply call the static property.

For example,

AppConfig.ConfigValue1()
AppConfig.ConfigValue2() 

This is turn returns the value stored in the static backing fields m_field1, m_field2

Problem is sometimes these values are empty string, when clearly the Web.Config entry has values.

Is there something fundamentally wrong with the above design, or is it reasonable to expect the static properties would keep their value for the life of the Application session?

© Stack Overflow or respective owner

Related posts about application

Related posts about session