更改asp.net会话状态模式会丢弃会话数据

时间:2014-04-09 20:27:03

标签: asp.net session session-state

我们正在使用asp.net会话来存储有关用户执行的长期流程进度的数据。 出于某些原因,我们必须暂时从 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 模式下运行良好。 可能是问题的根源是什么?感谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

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

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

这个初始化可能会写入&#39; sessionItem&#39; null 因为无法立即将其投放到词典中。在获取“ProgressStatus&#39;

”时,请尝试验证您的类型