Can ASP.NET session live longer than Application
Asked Answered
C

2

5

This could be a silly/lame question, especially after working so long with ASP.NET :), but I need to be sure.

Is it possible to have session (that is ASP.NET session) outlive the Application (app instance/app domain/Application variable)?

In other words, if Application_End is called in the Global.asax, does it indicate that there will be no more active session? and any new request will result in a Application_Start followed by an new Session_Start?

Note, the Session may not always be InProc, the session could be in a State server or SQL server.

Callipash answered 7/9, 2011 at 22:51 Comment(0)
L
5

With the default InProc session state, the application will terminate when the last session has expired, at which point Application_End occurs. In this scenario the entire appDomain is torn down and all memory freed. As sessions are persisted in memory they are permanently destroyed at this point, and therefore can never live beyond the life of the application.

If using Sql Server or State Server where the session is stored on a separate machine, then when the application is torn down the sessions can continue to live. Then because the client retains the original session cookie in the browser, the next time they visit the site the session is restarted, and sessionid used to identify their existing session.

Laurenlaurena answered 7/9, 2011 at 22:54 Comment(4)
I am looking for the scenario from other direction. What happens if application terminates (say by restarting IIS or process recycling etc), will session be still active?Callipash
Well if you store session in a database or state server then yes, the client will still have a session cookie. So next time they visit and the application starts up their session can be restarted (because the session data still persists against the sessionid).Laurenlaurena
Hmm, thanks. I was trying to save some info regarding a session into application (static variable). But looks like we cannot guarantee that the static variable will always have value for every active session. This is sad :(Callipash
Statics get lost when the application ends anyway. It's a bad idea.Laurenlaurena
C
4

Yes, when you put the state in SQL Server the application could restart but you will still maintain the session state

Comestible answered 7/9, 2011 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.