20 August 2005

Disappearing Session Variables

Posted by Mikhail Esteves under: C#; Tips; Windows .

ASP.NET has a very annoying habit of loosing it’s session variables. Sometimes it exists on every page, sometimes sessions are shared among all visitors of the site, sometimes sessions seem to disappear in just one page but work fine in the rest, sometimes the session just stays in one page of the site even though you’ve logged out …

There are many reasons any these problems could happen and I’ve listed a few things you can try.

  1. Try using StateServer or SQL to manage sessions instead of InProc.
  2. Check to see if your server’s name has an underscore or any illegal characters in it. If it does, get rid of them.
  3. Ensure there are no files except for the DLL’s in your bin directory. If you have a constantly accessed files, like a database or log files, get it out of there right away. .NET monitors the bin folder for changes and restarts when there is a change. Every restart results in your session variables disappearing.
  4. Disable your anti-virus software and see if the problem goes away. It’s possible that is interfering with the bin directory.
  5. Take a look at this
  6. Turn off the IIS Recycling Feature if you use it and see if it solves your problem.
  7. Try using a session check (if (Session["me"] != null) bool IsSession=true;) and use this value to work the page. This sometimes works, but not always.
  8. Disable cookieless sessions in web.config and see if that helps.
  9. Open the IIS Management Console, go to Application Pools, then Performance and change the setting “Maximum number of Worker Processes” under “Web garden” to “1”. Try now.

If none of these work, you can also try tracking Session IDs. In the first page where you set session variables (say the login page), store the session ID in a database for the user. Every page thereafter should compare their session ID to the one that was given to the user. Just in case any of the new ones are not unique, use Session.Abandon and get a new SessionID.

Technorati Tags , , , ,


One Comment so far...

Tracie Spillar Says:

17 February 2006 at 2:13 am.

Tip 9 has saved my life!!!!! Thank you so much. We have been chasing this for two days when I happened on your tip. Setting the Web Garden to zero made all our problems go away.

Leave a Reply

Browse

Photography

Projects

Pages

Calendar

August 2005
M T W T F S S
« Jul   Sep »
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

www.flickr.com

Use OpenDNS