Reputation: 8132
I have one ASP.Net MVC - 5 application and in one of the place I have stored value in Session as
System.Web.HttpContext.Current.Session["MyValue"] = user.SessionID;
I am able to access the value as
int x = (int)System.Web.HttpContext.Current.Session["MyValue"];
But I want to access the same value when I stop the IIS Express. I know that Application_End
gets called from Global.asax
.
protected void Application_End()
{
int x = (int)System.Web.HttpContext.Current.Session["MyValue"];
}
But the value of x comes out to be null. Is there any particular reason that session value is not available in Application_End(). Am I missing something here?
Upvotes: 0
Views: 348
Reputation: 6530
The session is already disposed by the time Application_End is called. Try using Session_End if you need to get information before IISExpress has totally stopped.
Upvotes: 2
Reputation: 1039408
By default sessions are stored in-memory (InProc
) mode. When your application shuts down all the memory associated with it is wiped. This means that everything you might have stored in the session is gone. That's the reason why in production you should absolutely never use InProc
mode but rather off-load the storage to a separate service or SqlServer. If for some reason your application is recycled by IIS (for example due to some inactivity or high load), then all session data is gone.
Upvotes: 1