7

I have a solution 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?

3
  • 3
    Well, from a "separation of concerns" standpoint it seems like the "other" project shouldn't be directly aware of the web environment. Can you just pass in whatever values from the session into the "other project" that are necessary? Commented Sep 30, 2011 at 0:04
  • Try : System.Web.HttpContext.Current.Session["varName"] . Also, be mindful of what stage in the ASP.NET page life cycle you are trying to read session - e.g. is it possible that session hasn't been loaded yet at the time you try to read it ? Commented Sep 30, 2011 at 6:07
  • Moe - I am customizing the logo that goes on the login screen depending on what URL is used to access the system. When I step through, I set one break point in the global.asax and it stops first and the variables are set. Then the next break point is when my login module tries to load the logo from the database. It calls a procedure from that 'other' shared class to handle the image load and the database name is null. I suppose I could try to take the session variable from the module where it is working and pass it to the method int he class that loads the image... I'll try that. Commented Sep 30, 2011 at 15:38

3 Answers 3

6

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Anne, but that's not my problem. I am using SQLServer mode. And I can use the Session while in my main project. It is when I try to access the Session vars in an external project (in the same solution) that I have an issue.
5

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.