Harry
Harry

Reputation: 1353

IIS ASP.net Session leaks / loses values 5% of the time

I'm working with an IIS V6.0 and ASP.NETwith .NET Framework 3.5 SP1.

Most time of the day I'm fixing issues relating to ASP.NET losing Session Variables betweeen requests. It's frustrating.

For example: I have Page A and Page B. While A is giving B an object of an own class with

Session["something"] = myObject; //on Page A 

and Page B wants to use it like that:

MyOwnClass myObject= Session["something"] as MyOwnClass;

This works about 95% of the time. But the other 5% myObject on B is null, while refreshing page again it might be the Object I put to Session again.

How is that possible? What can I do about it?

It's occuring over different browsers. So even the companys IE7 shouldn't be part of the problem. I tried various Session TimeOut Lengths but...nothing.

Upvotes: 0

Views: 757

Answers (2)

Zach Green
Zach Green

Reputation: 3460

My first guess is that you are using InProc session state, and when the application pool recycles, you lose what is in session.

Here is guide for II6 to change InProc to StateServer: http://dotnetguts.blogspot.com/2009/06/steps-for-session-inproc-mode-to.html

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

probably "sometime" myObject is not a MyOwnClass type and because you are using a safe cast if this happen you won't get an exception(the safe cast will return null is the object is not "Castable". try to use an explicit cast instead:

try{
    MyOwnClass myObject=(MyOwnClass)Session["something"];
   }
catch(InvalidCastException ex){
    //handle the exception
    }

if this does not solve your issue you have to make sure:

  • You don't restart the application pool
  • you don't change anything in your web config
  • webfarms and web gardens: if you have configured web farms and web garden for your web site. Inproc session sharing can cause issues.
  • w3p process of your website is getting restarted because of some issue in code. or memory leaks.

Upvotes: 3

Related Questions