Unbreakable
Unbreakable

Reputation: 8132

How to fetch session stored value when IIS Express stops

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

Answers (2)

Fran
Fran

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.

https://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.end(v=vs.110).aspx

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

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

Related Questions