【发布时间】: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