【问题标题】:The session state Information is invalid and might be corrupted in ASP.Net会话状态信息无效,可能在 ASP.Net 中已损坏
【发布时间】:2015-07-14 18:41:37
【问题描述】:

我正在使用带有 C# 的 ASP.Net 3.5,开发 ID:Visual Studio 2008。当我使用时

Session["FileName1"] = "text1.txt" 

它工作正常,但我正在使用

number1=17;
string FileName1="FileName1" + number1.toString(); 

然后设置

Session[FileName1]="text1.txt";

给我运行时错误

会话状态信息无效,可能已损坏 在 System.Web.SessionState.SessionStateItemCollection.Deserializer(BinaryReader 阅读器)

当我在Session 变量中使用字符串时,谁能解决我的问题?请记住,它可以在我的开发机器上运行(即本地 Visual Studio),但在部署到服务器时会出现上述错误。

【问题讨论】:

  • 它适用于我的本地视觉工作室,但它在我部署它的服务器上出现错误。
  • 试试string FileName1="text1.txt" + Guid.NewGuid().ToString();Session.Add(FileName1, "text1.txt");
  • 能否请您包含此异常的堆栈跟踪?
  • 它是一个网络农场还是只有一台服务器?
  • @Koen 只有 1 台服务器..!

标签: asp.net session-variables session-state


【解决方案1】:

在尝试通过 Session[FileName1] 语法访问之前,确保 FileName1 变量不为空...

以下是遇到相同问题的其他人的链接: http://forums.asp.net/t/1069600.aspx

这是他的答案:

在代码中,我找到了以下行:

//some code
Session.Add(sessionVarName, sessionVarValue);
//some other code

显然,由于一些脏数据,有一段时间 sessionVarName 为空。

Session.Add 在这种情况下不会抛出任何异常,如果你的 Session Mode是“InProc”,不会有问题。但是,如果您的 会话模式为“SQLServer”,在会话反序列化期间 商店,你会得到我得到的例外。所以,过滤掉脏东西 数据,我将代码修改为:

if (sessionVarName != null)
{
  //somecode
  Session.Add(sessionVarName, sessionVarValue);
  //some other code
}  

【讨论】:

    【解决方案2】:

    你的错误原因是

    xyz = new Guid() is also xyz= Guid.Empty;
    

    所以当你尝试转换为字符串时,它会抛出错误。

    只需修改类似的代码即可。

    Guid guId = System.Guid.NewGuid(); 
    string x = guId .ToString();
    string FileName1="text1.txt" + x;
    Session[FileName1]="text1.txt";
    

    【讨论】:

    • 对不起我修改了我没有使用 Guid 我使用的是整数说“FileName1”+数字
    • 我认为你的问题在这一行---- string FileName1="FileName1" + number1;它将是字符串 FileName1="FileName1" + number1.toString();如果您有其他查询可以询问。
    • HttpContext.Current.Session[FileName1] ="text1.txt";试试这个
    【解决方案3】:

    在将它们存储在会话中之前检查您的值,它们可能会在会话存储的反序列化过程中导致此异常,过滤您的数据。 检查Here

    if(!string.IsNullOrEmpty(FileName1))
    {
      Session.Add(FileName1, "text1.txt");
    }
    

    或者检查字符串中的无效字符。

    【讨论】:

    • @VaibhavParmar 你的会话状态模式是什么?
    • 我使用了 In-proc session-state 模式
    【解决方案4】:

    您可以像这样将值添加到会话中

        string FileName1="FileName1" + number1.toString(); 
    
    if(!string.IsNullOrEmpty(FileName1))
    {
      Session.Add(FileName1, "text1.txt");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多