简体   繁体   English

ASP.NET C# 会话变量丢失

[英]ASP.NET C# Session Variables Being Lost

I have a solution which includes 2 projects and 2 class files that are called by reference.我有一个解决方案,其中包括 2 个项目和 2 个通过引用调用的类文件。 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.在我的一个项目中,我在 Session_Start 块中的 Global.asax 中有一些代码,它从数据库加载一些变量并将它们设置为会话变量。 If I put a break point in the Global.asax I can verify that the variables are in fact being set correctly.如果我在 Global.asax 中放置一个断点,我可以验证变量实际上是否设置正确。

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.但是,如果我在通过引用调用的类之一(本质上是共享类)中引用它们,则 Session 变量都是空的。

I am using HttpContext.Current.Session["varName"] to access the variables in a class, as is standard.我正在使用 HttpContext.Current.Session["varName"] 来访问类中的变量,这是标准的。

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 .我发现这篇文章对我的问题很有帮助: 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?你在代码的任何地方调用 Session.Abandon() 吗? I was doing this at the start of my Web App to ensure I was starting with a "fresh" session.我在我的 Web 应用程序开始时这样做是为了确保我从“新鲜”会话开始。 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.事实证明,即使“放弃”之后存储的任何 Session 变量也将被删除(即使 SessionID 被迫通过其他方式保持不变,例如使用 Server.Transfer(Url, true) 而不是 Response.Redirect),回发。

ie 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.即我可以跟踪到我的应用程序,观察所有会话变量是否正确设置,然后在调用任何事件处理程序(任何带有 AutoPostBack="True" 的东西,例如 UpdatePanel 上的复选框或按钮)时,BAM,我有相同的 SessionID,但会话变量为零。

Removing the pre-emptive call to Session.Abandon() solved my problem straightaway.删除对 Session.Abandon() 的先发制人调用直接解决了我的问题。

Jeff杰夫

Check Maximum Worker Process property set to 1 in your AppPool Advanced Settings window.AppPool高级设置窗口中检查Maximum Worker Process属性设置为 1。

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.如果您无意中设置大于 1,应用程序池将是一个Web Garden因此您无法找到SessionApplication变量,即使它们仍然存在于其中之一中。

Click here for more information 点击这里查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM