【问题标题】:Changing asp.net session state mode drops session data更改 asp.net 会话状态模式会丢弃会话数据
【发布时间】:2014-04-09 20:27:03
【问题描述】:

我们正在使用 asp.net session 来存储有关用户执行的长期过程的进度的数据。 由于某些原因,我们不得不暂时从 InProc 模式切换到 SQLServer 会话状态模式。结果数据被写入会话

public static void UpdateProgressInSession(HttpContext context, string guid, int progress)
    {
        var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
        if (sessionItem == null)
        {
            context.Session["ProgressStatus"] = new Dictionary<string, int> { { guid, progress } };
            return;
        }
        sessionItem[guid] = progress;
        context.Session["ProgressStatus"] = sessionItem;
    }

在具有相同ID但从另一个线程的会话下不可读。

 public TestRunnerSession GetSessionProgress(HttpContext context, string guid, int testSessionsCount)
    {
        var testRunnerSession = new TestRunnerSession()

        //this variable is always null from now even after session was updated
        var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
        if (sessionItem != null && testSessionsCount >= MaxSessionsCount && !sessionItem.Keys.Contains(guid))
        {
            testRunnerSession.Status = Currently.Waiting;
            testRunnerSession.ProgressValue = 0;
            return testRunnerSession;
        }
        int value;
        if (sessionItem != null && sessionItem.Count != 0 && sessionItem.TryGetValue(guid, out value))
        {
            testRunnerSession.Status = Currently.InProgress;
            testRunnerSession.ProgressValue = value;
            return testRunnerSession;
        }

        //sessionItem has not been initialized yet
        //This part of the method is always fired now
        testRunnerSession.Status = Currently.InProgress;
        testRunnerSession.ProgressValue = 0;
        return testRunnerSession;
    }

此代码在 InProc 模式下运行良好。 问题的根源可能是什么?谢谢

【问题讨论】:

    标签: asp.net session session-state


    【解决方案1】:
    【解决方案2】:

    我认为问题不在会话中... 查看这段代码:

    var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
    

    可能这个初始化写入了“sessionItem”null,因为不能立即将其转换为 Dictionary。获取“ProgressStatus”时尝试验证您的类型

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2021-03-03
      • 2013-04-30
      • 2011-07-21
      • 2019-07-18
      • 2016-10-21
      • 2011-03-31
      • 2011-06-07
      相关资源
      最近更新 更多