关于sessionState的mode=StateServer的问题

C5662601 2010-07-28 04:16:50
页面有一button
点击事件里 1初始化Session["a"] = 0 2启动一个线程 3启动一个timer
在线程里会给Session["a"]的值每隔一段时间+1

当mode=InProc时
在timer事件里可以访问到当前Session["n"]的值不断变化
当mode=StateServer时

在timer事件里 访问当前Session["n"]的值始终为0

是什么原因造成的?
...全文
660 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
C5662601 2010-07-28
  • 打赏
  • 举报
回复
.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Timer1_Tick(object sender, EventArgs e)
{
string str = Session["a"].ToString();
Label1.Text = str;
}

protected void Button1_Click(object sender, EventArgs e)
{
Session["a"] = 0;
Timer1.Enabled = true;
Timer1_Tick(null, null);
Thread t = new Thread(new ThreadStart(ThreadToRun));
t.Start();
}

private void ThreadToRun()
{
int num = 0;
while(true)
{
num = num + 1;
Session["a"] = num;
Thread.Sleep(1000 * 5);
}
}
}
}
C5662601 2010-07-28
  • 打赏
  • 举报
回复
aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer id="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
C5662601 2010-07-28
  • 打赏
  • 举报
回复
web.config
<?xml version="1.0" encoding="utf-8"?><configuration>

<appSettings/>
<connectionStrings/>

<system.web>

<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation>

<authentication mode="Windows"/>

<httpHandlers>
<remove verb="*" path="*.asmx" />
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>
<!-- InProc StateServer -->
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" stateNetworkTimeout="14400"/>
</system.web>
</configuration>
wuyq11 2010-07-28
  • 打赏
  • 举报
回复
StateServer
设置为将Session存储在独立的状态服务中。通常是aspnet_state.exe进程.
获取session状态的速度比InProc慢一些
C5662601 2010-07-28
  • 打赏
  • 举报
回复
希望有空闲的朋友能按我的叙述实践下
页面有一button  
点击事件里 1初始化Session["a"] = 0 2启动一个线程 3启动一个timer
在线程里会给Session["a"]的值每隔一段时间+1

当mode=InProc时
在timer事件里可以访问到当前Session["n"]的值不断变化
当mode=StateServer时

在timer事件里 访问当前Session["n"]的值始终为0

C5662601 2010-07-28
  • 打赏
  • 举报
回复
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" stateNetworkTimeout="14400" />
断点跟踪在线程里都可以访问到真实的Session["a"]的值
[Quote=引用 4 楼 archu 的回复:]
你先看看基本的session功能是否正常,即:往Session里面存放一个变量,再访问另一个页面看能否取到此变量。

我估计是你的程序连不到State Server,把web.config里面相关的配置贴出来。
[/Quote]
symbol_bc 2010-07-28
  • 打赏
  • 举报
回复
等高手解答
archu 2010-07-28
  • 打赏
  • 举报
回复
你先看看基本的session功能是否正常,即:往Session里面存放一个变量,再访问另一个页面看能否取到此变量。

我估计是你的程序连不到State Server,把web.config里面相关的配置贴出来。
C5662601 2010-07-28
  • 打赏
  • 举报
回复
最简单的线程写法
断点跟踪在线程里去Session["a"]的值都是正确的
在timer时间里俩种情况就各不相同了[Quote=引用 2 楼 myhope88 的回复:]
应该跟你写的线程有关系吧,具体什么原因我也说不上来
[/Quote]
myhope88 2010-07-28
  • 打赏
  • 举报
回复
应该跟你写的线程有关系吧,具体什么原因我也说不上来
yang_5 2010-07-28
  • 打赏
  • 举报
回复
不懂 期待中

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧