Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an application pool recycle affect ASP Net Session State?

Tags:

asp.net

iis

I know when application pool is recycled a new worker process is initiated but I am confused with how expired and valid sessions are treated in this process? which passes to new worker process thread to and which are eliminated? what it will do with:

  • User A's session has expired
  • User B's session is valid

after recycling, when User A and User B will request then what will be their session statet?

like image 832
Umer Hayyat Avatar asked Dec 31 '15 10:12

Umer Hayyat


1 Answers

If you have a single web server, and you've used the default InProc mode for SessionState persistence, then any data that you've added to the session's Dictionary in your server code will be lost during an App Pool recycle - after the recycle, when your code next accesses an entry in the SessionState dictionary, it will return null.

This will similarly happen if you have multiple web servers across a load balancer, with session state incorrectly configured as InProc, and the user returns to a different server (i.e. without sticky routing).

(The Session State cookie stored on the browser may still be valid for several minutes yet).

The way to allow Session State to 'survive' an App Pool recycle, server crash, or to span across a farm of servers is to persist data stored in SessionState, so that the server (or servers) can again retrieve the data when the user's session returns. Easiest is to use one of the out of the box solutions, viz a separate StateServer process, or store state in a SqlServer database. Custom persistence is also an option.

One caveat - note that any data that you store in an 'out of process' mode such as StateServer or SqlServer needs to be serializable - this can be a breaking change when you switch out of InProc.

like image 124
StuartLC Avatar answered Oct 30 '22 03:10

StuartLC