2010-05-18 2 views
3

다음 디자인에 근본적으로 잘못된 것이 있습니까? 아니면 정적 속성이 때때로 값을 잃어 버리는 이유를 알 수 있습니까?정적 속성 값이 간헐적으로 손실됩니까?

클래스 AppConfig가 포함 된 클래스 라이브러리 프로젝트가 있습니다. 이 클래스는 Webforms 프로젝트에서 사용됩니다.

다음과 같이 appconfig가 클래스의 골격은 다음과 같습니다

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 

정적 속성은 그 후 Global.asax에

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

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

End Sub 

의 Application_Start 이벤트에서, 응용 프로그램 시작에 한 번 설정 얻을 때마다 우리를 어디에서든 Web.Config의 값에 액세스하려고합니다. aspx 페이지 코드 숨김 또는 다른 클래스 또는 참조 된 클래스를 호출 할 때는 단순히 정적 속성 만 호출하면됩니다.

AppConfig.ConfigValue1() 
AppConfig.ConfigValue2() 

이 차례가 정적 백업 필드 m_field1에 저장된 값을 반환 예를 들어

, m_field2

문제는 명확의 Web.config 항목이있는 경우이 값은 빈 문자열입니다 때때로 값.

위의 디자인에 근본적으로 잘못된 것이 있습니까? 아니면 정적 속성이 응용 프로그램 세션의 수명 동안 가치를 유지할 것으로 기대하는 것이 합리적입니까?

답변

0

정적 속성은 Global.asax에

의 Application_Start 이벤트에서, 응용 프로그램 시작에 한 번 설정 얻을 사실 그게 문제였다. 당신으로 session_start에 정적 소품을 설정하면, 언제든지 느슨한 준비 (그들은 다시 나타날 수도 다시!) : 코드의주의 깊은 검사가

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

결론에 설정되고 있었다 revelead.

Application_Start로 이동했는데 문제가 해결되었습니다.

이제 IIS의 내부 동작에 대해 누가이 시나리오가 발생했는지 설명 할 수있는 그럴듯한 설명을 제공 할 수 있습니까?

0

나는 디버그/추적 출력을 추가하여 어떤 일이 벌어지는지를 볼 수 있습니다. 또한 예외를 추적해야합니다.

또한 현대 구문을 사용하기 위해 속성 반환을 변경했습니다 (어떻게 더 잘 지원되는지 잘 모르겠습니다).

return defaultValue 

대신 :

ReadSetting = defaultValue 
관련 문제