To clarify one point first: HttpContext.Current
is still available in IIS 7, it's just not available in Application_Start
because there is not necessarily an incoming request from an external user that causes the event to fire.
Now, to the problem:
You did not show how DB_Environment
is defined, but I suspect it is just a standard Property, i.e.
Public Property DB_Environment As String
If this is the case, you can easily and seamlessly address your problem by changing DB_Environment to a readonly property with a backing member variable and then perform the initial configuration if the backing store property has not been set. In this design, it will only be initialized when needed and it should only be needed when an external user makes a request, thus ensuring an HttpContext.
Here is the proposed design:
Private m_DB_Environment As String = Nothing
Public ReadOnly Property DB_Environment As String
Get
' If we haven't retrieved the setting yet and there is a current context,
' retrieve the appropriate setting
If m_DB_Environment Is Nothing AndAlso HttpContext.Current IsNot Nothing Then
Select Case LCase(HttpContext.Current.Request.ServerVariables("SERVER_NAME"))
Case "localhost", "development_domain"
m_DB_Environment = "DEVELOPMENT"
Case "staging_ip", "staging_domain"
m_DB_Environment = "STAGING"
Case "external_prod_domain", "external_prod_ip", "internal_prod_domain"
m_DB_Environment = "PRODUCTION"
End Select
End If
Return m_DB_Environment
End Get
End Property