zoukankan      html  css  js  c++  java
  • 关于多台机器之前session共享,sessionState mode="StateServer" 问题的困扰

    .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_MACHINESYSTEMCurrentControlSetServicesaspnet_stateParameters] 配置端口(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

  • 相关阅读:
    控制台内容保存为文件
    SpringBoot
    JAVA基础
    jenkins的.gradle目录结构说明和清理
    macos 签名+公证app生成dmg后,安装使用过程中崩溃
    MacOS命令行打包+签名+公证+生成dmg文件
    jenkins构建调用tar报错:tar: Failed to set default locale
    jenkins构建报错:appdmg: command not found
    jenkins 构建xcode-select -s 切换xcode版本失败 (切换xcode路径无效)
    jenkins 执行shell编译go 代码报错:build cache is required, but could not be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME are defined
  • 原文地址:https://www.cnblogs.com/w3live/p/5570406.html
Copyright © 2011-2022 走看看