0

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.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Harry
  • 1,313
  • 3
  • 22
  • 37
  • 2
    what are you using to store session? InProc, StateServer, or SQL Server? – Zach Green Feb 02 '12 at 12:55
  • Do you use InProc session manager ? If yes this is your issue, move it to SQL – Aristos Feb 02 '12 at 12:57
  • how can i find out? @ZachGreen (its not SQL) – Harry Feb 02 '12 at 13:01
  • Ok! Its InProc, debugged my Application to find this out. – Harry Feb 02 '12 at 13:06
  • @Harry You can see this on web.config. Setup an sql server session to solve your issues – Aristos Feb 02 '12 at 13:08
  • @Aristos, SQL server is often the best practice for large applications that are load balanced, but it may be overkill in Harry's situation. Harry, here is a SO question talking about the pros and cons of each: http://stackoverflow.com/questions/1447175/sqlserver-vs-stateserver-for-asp-net-session-state-performance – Zach Green Feb 02 '12 at 14:54

2 Answers2

3

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.
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
2

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

Zach Green
  • 3,421
  • 4
  • 29
  • 32
  • that sounds interesting, so it feeling like its getting worse as more people are using my site, what would you advice me to do? – Harry Feb 02 '12 at 13:02
  • You can check in IIS to see if the site is using InProc as the session handler. If it is, you could switch to state server (make sure asp.net state server is started in services) to see if that clears up the problem. – Zach Green Feb 02 '12 at 13:08
  • This appears to have the steps for IIS 6: http://dotnetguts.blogspot.com/2009/06/steps-for-session-inproc-mode-to.html – Zach Green Feb 02 '12 at 13:08
  • What about using InProc would cause the session to empty 95% of the time? The OP has made no mention of anything that would lead me to think app pools are being recycled. The answer by Massimiliano Peluso suggests a reason that your solution would fail to solve. – ColinRobertson Feb 02 '12 at 15:06
  • He says it empties 5%, not 95%. – Zach Green Feb 02 '12 at 15:14
  • I'm implementing SQLServer Session till monday, we'll see if it works :) @ColinRobertson – Harry Feb 02 '12 at 15:47
  • @ZachGreen, sorry was just a typo. My question still stands though, what about InProc causes the session to empty 5% of the time? I'm not saying you are wrong, just a query as I've never had any problems with InProc. – ColinRobertson Feb 02 '12 at 16:19
  • @ColinRobertson, and I am not saying I'm right either, just a possible solution. If his application pool is recycling due to a regular recycle schedule, due to a limit being exceeded (for example, virtual memory), or some other cause, the contents of session will be lost because the worker process is restarted. http://blogs.msdn.com/b/webtopics/archive/2009/07/22/in-proc-session-state-management.aspx – Zach Green Feb 02 '12 at 16:33