Session value set in Session_Start not available in Page_PreInit
<p>I am having an intermittent problem with an ASP.NET 4.0 Web Forms application.</p>

<p>In Session_Start, I store the master page file path in the session:</p>

<pre><code>protected void Session_Start(Object sender, EventArgs e)
{
   // Not shown: get master page path from database

   Session["MasterPagePath"] = PathIGotFromTheDatabase;
}
</code></pre>

<p>Then in my pages' Page_PreInit, I use that session value to set the Page.MasterPageFile</p>

<pre><code>protected void Page_PreInit(object sender, EventArgs e)
{
    Page.MasterPageFile = Session["MasterPagePath"] + @"/MyMasterPage.Master";
}
</code></pre>

<p>This works 99% of the time, but occasionally something breaks, and Session["MasterPagePath"] is null. The users report that they have to close all of their active browser sessions in order to use the site again.</p>

<p>My understanding is that since I populate the Session["MasterPagePath"] in Session_Start, it should always be available in my pages' PreInit method. If my session had expired, it would always be repopulated by Session_Start before Page_PreInit is called.</p>

<p>Am I missing something here? Under what conditions could what I describe happen? I am using InProc session state for what it's worth.</p>
