ASP.NET Secrets, Part 5, Page 2
What to Do When Session_End Doesn't Work
It's one of the most common complaints on the newsgroups: "Hey! My Session End event doesn't fire. The code I want to run when the Session finishes just never runs." And then you get the whiz kid that replies with: "It's a bug. You can't rely on it on that End event. Use another method."
Important note: the whiz kid is wrong.
If the code you've entered in your Global.asax file to run when the Session End event fires doesn't appear to be kicking in, there are just three things that could be at fault:
- You're not using InProc—The mode attribute of the <sessionState> element in your application Web.config file must be set to "InProc". If it isn't, the End event simply cannot fire.
- You're not using the Session—The Session End event can't fire if there's no Session to End. Are you sure your application is storing information through the Session object?
- Your code is flawed—There's an error somewhere in the code that responds to the End event. Try running your application in debug mode, with a short <sessionState> timeout attribute. Visit the page and then wait. Does the End event kick in? Does an error occur in your code?
There is quite seriously no other excuse for your code not running. There's no mysterious bug. There are no weird setup issues or reasons for not trusting the Session End event. It works, no excuses.