Depending on your configuration, there can be many causes for application variables to suddenly become unpopulated.
- re-saving global.asa
- loading / unloading the application or web site
- rebooting the server / tripping on the power cord
- stopping / starting the web service (W3SVC or IISADMIN)
- issuing an errant Application.Contents.Remove() or .RemoveAll()
Now, if you have code in ASP pages that rely on these application variables being populated, you should have a contingency plan. When confronted with this situation, I used a separate ASP page which could be hit directly to regenerate the application variables from scratch. This file was also included in global.asa (to reduce redundancy and ensure consistency), in the application_onStart event, as described in
Article #2144. And to allow applications to redirect to this paeg and then get sent back to the referer, it itself is also included in another ASP page.
Basically, it goes something like this. In your ASP page that *depends* on the application variables, you have this logic:
<% if Application("StringVariable") = "" then response.redirect("/globalInclude.asp") else StringVariable = application("StringVariable") end if ' ... continue page %> |
Then globalInclude.asp looks something like this:
<!--#Include file=global.asp--> <script language=VBScript runat=server> ref = Request.ServerVariables("HTTP_REFERER") if ref <> "" then Response.Redirect(ref) else Response.Redirect("/") end if </script> |
And finally, global.asp looks like this:
<script language=VBScript runat=server> ' clean up any remaining bits Application.Contents.RemoveAll() ' okay, now start populating the app vars Application("StringVariable") = "foo" ' ... </script> |
In global.asa, of course, you would only include global.asp in the application_onStart() method, since otherwise it would try to redirect, which is not allowed in global.asa.