更改asp.net会话状态模式会删除会话数据
我们使用asp.net会话来存储有关用户执行的长期过程的进度的数据。 由于某些原因,我们不得不暂时从InProc模式切换到SQLServersessionstate模式。因此,将数据写入会话更改asp.net会话状态模式会删除会话数据,asp.net,session,session-state,Asp.net,Session,Session State,我们使用asp.net会话来存储有关用户执行的长期过程的进度的数据。 由于某些原因,我们不得不暂时从InProc模式切换到SQLServersessionstate模式。因此,将数据写入会话 public static void UpdateProgressInSession(HttpContext context, string guid, int progress) { var sessionItem = context.Session["ProgressStatus
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;
}
publicstaticvoidupdateprogressession(HttpContext上下文、字符串guid、int进程)
{
var sessionItem=context.Session[“ProgressStatus”]作为字典;
if(sessionItem==null)
{
会话[“ProgressStatus”]=新字典{{guid,progress};
返回;
}
sessionItem[guid]=进度;
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;
}
公共TestRunnerSession GetSessionProgress(HttpContext上下文、字符串guid、int TestSessionCount)
{
var testRunnerSession=newtestrunnersession()
//从现在起,即使在更新会话之后,此变量也始终为空
var sessionItem=context.Session[“ProgressStatus”]作为字典;
if(sessionItem!=null&&testsessionCount>=MaxSessionCount&&!sessionItem.Keys.Contains(guid))
{
testRunnerSession.Status=当前正在等待;
testRunnerSession.ProgressValue=0;
返回testRunnerSession;
}
int值;
if(sessionItem!=null&&sessionItem.Count!=0&&sessionItem.TryGetValue(guid,out值))
{
testRunnerSession.Status=Currently.InProgress;
testRunnerSession.ProgressValue=值;
返回testRunnerSession;
}
//sessionItem尚未初始化
//方法的这一部分现在总是被激发
testRunnerSession.Status=Currently.InProgress;
testRunnerSession.ProgressValue=0;
返回testRunnerSession;
}
此代码在InProc模式下运行良好。
问题的根源是什么?谢谢我想你可以很有希望地找到这个链接,其中aspnet会话状态得到了很好的解释我想这个问题不在会话中。。。 查看以下代码:
var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
var sessionItem=context.Session[“ProgressStatus”]作为字典;
可能这个初始化写入了“sessionItem”null,因为不能立即将它转换到字典中。获取“ProgressStatus”时,请尝试验证您的类型