ASP.NET C# Session Variables Being Lost
I have a s开发者_JAVA百科olution which includes 2 projects and 2 class files that are called by reference. In one of my projects, I have some code in Global.asax in the Session_Start block that loads a few variables from a database and sets them into session variables. If I put a break point in the Global.asax I can verify that the variables are in fact being set correctly.
When I reference the session variables in classes in any of my code-behind modules or a class in my project, they are there. But if I reference them in one of the classes that is called by reference (a shared class, essentially), the Session variables are all null.
I am using HttpContext.Current.Session["varName"] to access the variables in a class, as is standard.
Is there something else I need to consider to have access to these session variables? Could it perhaps be a namespace issue?
I had the same issue before, I keep on losing the my session variables (although not in the same context as yours). I found this articles helpful for my issue: ASP.NET Case Study: Lost session variables and appdomain recycles and PRB: Session Data Is Lost When You Use ASP.NET InProc Session State Mode. Hope it might help you too. Cheers!
Are you calling Session.Abandon() anywhere in the code? I was doing this at the start of my Web App to ensure I was starting with a "fresh" session. Turns out that any Session variables stored even after the "Abandon" would be dropped (even if the SessionID was forced to remain the same via other means, such as using Server.Transfer(Url, true) rather than Response.Redirect), upon postback.
i.e. I could trace into my app, watch all the session variables be correctly set, and then the moment any event handler (anything with AutoPostBack="True", like a checkbox or button on an UpdatePanel) was called, BAM, I had the same SessionID, but zero session variables.
Removing the pre-emptive call to Session.Abandon() solved my problem straightaway.
Jeff
Check Maximum Worker Process
property set to 1 in your AppPool
Advanced Settings
window.
If you unintentionally set greater than 1, the application pool will be a Web Garden
so you cannot find Session
nor Application
variables even though they still exists inside one of them.
Click here for more information
精彩评论