0

I noticed that while debugging my applications, sometimes the [InProc] Session State is destroyed following re-builds (C# Web Application). The sequence of events are as follows:

  1. Rebuild & run application (Debug or Release Mode, Doesn't Matter)
  2. Populate a Session Variable in Page_Load() Event
  3. Session_End() fires then Application_End fires()
  4. I perform a postback and check for Session variable populated in Step 2, it is empty.

I am running this application using IIS Express, but it seems to occur irregardless of which web server is being used. This is causing numerous problems as the application isn't counting on Session variables to vanish.

namespace BlankWebApp
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["test"] = true;
      }
    }

    protected void butCheckSession_Click(object sender, EventArgs e)
    {
      if (Session["test"] == null)
      {
        // Session_End and Application_End must have been called
      }
    }
  }
}
bperniciaro
  • 901
  • 2
  • 12
  • 30

1 Answers1

2

Changing content of bin folder will cause application pool recycling. This is what is happening with the Re-Build, as rebuild will compile the application and create a new dll/executables for the project causing changes in the bin folder. Application pool recycling will cause the session to be removed from server memory.

Similar is true for changing Web.Config file as well.

You can't avoid that. You should have a separate development and production environment.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • So any time I re-build during development (or any time that application pool recycles in production) I have to accept that the application will lose all Session variables populated in the Page_Load event? If so, where should I populate Session variable to ensure they won't be destroyed in this scenario? – bperniciaro May 21 '15 at 18:54
  • @bperniciaro In the database! or some other cache but never in Session. Session really should be avoided - this being the first reason of what you are experiencing. – Ahmed ilyas May 21 '15 at 18:55
  • @bperniciaro,`Out Proc` , see http://stackoverflow.com/questions/4277944/is-it-possible-to-recycle-iis-application-pools-without-losing-user-sessions and http://stackoverflow.com/questions/561705/best-practices-for-inproc-vs-stateserver-for-asp-net-session-state, also see if it is possible to avoid using sessions, remember they are maintained per user on the web server and could end up being expensive in term memory. – Habib May 21 '15 at 18:59
  • @Ahmedilyas In our scenario we are actually using the Session variable to determine when each user's session has expired. So on postbacks we check to see if the Session variable we initially populated is NULL and if so redirect the user to a 'Session Expiration' page. But users are getting redirected there prematurely when the App Pool recycles apparently. – bperniciaro May 21 '15 at 20:45
  • Nothing much you can do with this solution. Only thing is to have it out of proc i.e SQL Storage or use some caching mechanism such as AppFabric – Ahmed ilyas May 21 '15 at 20:48