您的位置:首页 > 其它

关于多台机器之前session共享,sessionState mode="StateServer" 问题的困扰

2016-06-08 16:12 337 查看
.net 多台机器共享session是很老的技术,一直很少用到session。

最近就出现了一个问题:三台前端,其中一台保存的session值死活不对,一样的环境,一样的配置文件,就是和另外两台获得的值不同(值总是等于每次访问这台机器时,执行写session操作的值)。

网上也搜了不少资料,不是什么详解,就是大全,找了好久(难道真的RP问题),终于发现解决方案,总结一下,给有需要的同学。

做好以下配置:

在web.config下,<system.web>节点下插入配置

<sessionState cookieName="DotNetAuth" mode="StateServer" stateConnectionString="tcpip=你的服务器IP(一般是内网):42424" cookieless="false" timeout="30" />


启动 Asp.Net Session Service 服务,在注册表 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters] 配置端口(Port,默认是42424),和允许远程连接(AllowRemoteConnection,默认是0,0-不允许,1-允许)

注:做好以上配置,不一定就可以成功共享session。因为你在IIS上创建的网站标识可能会不同,标识不同机器之间session是不能共享的(当然你运气好到爆,我也没话说)。



解决方案一:有条件的同学,可以保持三台服务器IIS上的站点标识ID都是一样(网站创建好,再修改不行,一定要创建时就是相同的标识ID),这样可以肯定三台机器的session之间是可以共享的

解决方案二:将园里的一段代粘贴到global.asax中,也可以解决(来至:http://www.cnblogs.com/cardgames/articles/3399546.html)

public override void Init()
{
base.Init();
foreach (string moduleName in this.Modules)
{
string appName = "APPNAME";
IHttpModule module = this.Modules[moduleName];
SessionStateModule ssm = module as SessionStateModule;
if (ssm != null)
{
FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
if (store == null)//In IIS7 Integrated mode, module.Init() is called later
{
FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
appNameInfo.SetValue(theRuntime, appName);
}
else
{
Type storeType = store.GetType();
if (storeType.Name.Equals("OutOfProcSessionStateStore"))
{
FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
uribaseInfo.SetValue(storeType, appName);
}
}
}
}
}


另附上一些参考资料:

sessionState详解:http://www.cnblogs.com/tangge/archive/2013/09/10/3312380.html

MSDN官方文档:https://msdn.microsoft.com/zh-cn/library/h6bb9cz9(VS.80).aspx

说说Asp.net的StateServer和Session共享:http://blog.csdn.net/ahywg/article/details/39232809
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: