Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:08:21+00:00 2026-05-14T23:08:21+00:00

Is there something fundamentally wrong with the following design, or can anyone see why

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:08:21+00:00Added an answer on May 14, 2026 at 11:08 pm

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

    Actually that was the problem. Careful inspection of the code revelead it was being set on

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            AppConfig.OnApplicationStart() 
    End Sub
    

    Conclusion: If you set static props on Session_Start, be prepared to loose them at any time (and they may re-appear again too!).

    I have moved it to Application_Start and the problem has gone away.

    Now can anyone provide a plausible explanation of the inner workings of IIS that may explain why i encountered this scenario?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something like the following in Apache Common Lang or Spring Utils or
This is my first attempt to use WCF so there may be something fundamentally
I must be doing something fundamentally wrong here. I've got very simple code: private
Is there something in EF 4.1 that allows for a reconnect to a database
Is there something new about row-level security in SQL Server 2012? In 2008 and
Is there something equivalent to OmniCppComplete for java in vim ? I know of
Is there something akin to the .NET xsd.exe for java - generating classes from
Is there something like serialize/unserialize PHP functions in jQuery? These functions return a string
Is there something like die in JavaScript? I've tried with break, but doesn't work
Is there something in the Android developer guidelines that disuades developers from providing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.