Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat system configuration or malfunction in a shared hosting environment can null read-only static variables?
    text
    copied!<p>I have an ASP.Net 4.0 website on a shared hosting system. Applications are recycled after 20 minutes of inactivity.</p> <p>In my application some static variables are initialized in application_start.</p> <p>When the next request comes in, the application should be restarted, application_start executed and the static variables reset.</p> <p>After the period of inactivity, when I access pages that depend on the static vars, they are null and that creates errors.</p> <p>Since these variables are initialized in application_start, it seems odd that after the period of inactivity that they would be null since the application restarting would reinitialize them.</p> <p>In fact, when I see the null reference errors, recycling the app pool (and executing application_start) solves the problem until the next time the application is dropped from memory.</p> <p>I'm wondering if some other type of system memory problem is occurring when the application is removed from memory, because if the application was simply being recycled, the reinitialization of the static variable in application_start would mean there would be no reason for the statics to be null.</p> <p>Since static variables are not garbage collected they should never be null after initialization.</p> <p>The errors never occur on any developer machines, only on the shared hosting system. What could be some other causes for static variables initialized in application_start to become null?</p> <p>There is no code that resets the variables. They are private fields that only contain access via a get method.</p> <p>Code:</p> <pre><code>private static List&lt;State&gt; stateList; public static void LoadStaticCache() { var service = DependencyResolver.Current.GetService&lt;ILocationService&gt;(); stateList = service.GetAllStates().ToList(); } public static List&lt;State&gt; GetStates() { return stateList; } </code></pre> <p>When the inactivity period is met, the next time the stateList is accessed, it is null.</p> <p>However, it is always initialized in application_start. How can it ever be null unless it is a system problem?</p> <pre><code> protected void Application_Start() { StaticCache.LoadStaticCache(); } </code></pre> <p>Recycling the Application Pool fixes the issue 100% of the time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload