为什么我不能分享2 Web应用程序使用的StateServer之间的会话状态?我在想什么? [英] Why can't I share Session state between 2 web apps with StateServer? What am I missing?

查看:155
本文介绍了为什么我不能分享2 Web应用程序使用的StateServer之间的会话状态?我在想什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦2个相同的ASP.NET MVC应用程序共享使用一个会话的StateServer同一个会话。我想这样做的原因是,我们最终将跨越需要共享相同的状态3的Web服务器上部署这个应用程序。我们需要使用StateServer,因为我们正试图尽量减少使用DB非数据相关的存储。

I'm having trouble getting 2 identical ASP.NET MVC applications to share the same Session using a Session StateServer. The reason I'm trying to do this is we will eventually be deploying this app across 3 web servers that need to share the same state. We need to use StateServer because we are trying to minimise use of the db for non data-related storage.

的设置:

我已经部署在同一code基地的http://本地主机/ App1的 HTTP://本地主机/ App2的

I've deployed the same code base to http://localhost/App1 and http://localhost/App2

两者具有相同的web.config文件中有以下内容:

both have identical Web.Config files with the following:

<system.web>
<sessionState mode="StateServer" 
              cookieless="false" 
              timeout="20" 
              stateConnectionString="tcpip=127.0.0.1:42424" />
              //stateConnectionString="tcpip=192.168.1.52:42424" /> // also doesn't work
<machineKey 
  validationKey="8B9F68D0CC730F6F046D0173021C34B1A0D9A01C21D8E4D4A7A1DFF38332DEE8CBBAFEA503C18776614EE9D4F7EEA7E5D2D5571630547D822485A27B1EF53AC1"
  decryptionKey="60009563EFCFC594FD1BC46684943AA398EE70412A624B2EB488BBB071F15ECF"
  validation="SHA1" decryption="AES" />

我用这个工具来生成这些计算机密钥

测试:

我把下面进入我的控制器之一,以测试它是否是工作:

I put the following into one of my Controllers to test if it was working:

ViewData["mode"] = requestContext.HttpContext.Session.Mode.ToString();

string timestamp = DateTime.Now.ToString();
if (requestContext.HttpContext.Session["timestamp"] == null)
{
    requestContext.HttpContext.Session["timestamp"] = timestamp;
}

ViewData["timestamp"] = requestContext.HttpContext.Session["timestamp"].ToString();
ViewData["realtime"] = timestamp;

这个视图:

<p>
    Mode: <%= ViewData["mode"].ToString() %>
</p>
<p>
    Time: <%= ViewData["timestamp"].ToString() %>
</p>
<p>
    real time: <%= ViewData["realtime"].ToString() %>
</p>

结果:

对于这两种部署,第一次加载页面时,我可以看到模式的StateServer和时间戳是越来越设置为相同的时间作为实时值。但是,如果这是工作,仅在第一页应该有同时作为实时值。第二个页面加载应该从阅读的StateServer因为时间戳值不再为空,并显示时间值。但是,相反,它再次显示实时值。

For both deployments, when the page first loads I can see that the mode is StateServer and the timestamp is getting set to the same time as the realtime value.. However, if this was working, only the first page should have the same time as the realtime value. The second page load should read from the StateServer because that timestamp value is no longer null, and display that time value. But instead, it's displaying the realtime value again.

在我刷新页面,我的时间戳保持不变,并实时值始终更新。这表明,时间戳被保存到会话,但时间戳值是为部署总是不同的,当它应该是相同的,因此这表明会话没有被共享。

When I refresh the page, I the timestamp stays the same and the realtime value is always updating. This indicates that the timestamp is being saved to the Session, but the time stamp value is always different for both deployments when it should be the same, so this indicates that the Session is not being shared.

可有人指出,如果我做错了什么,或者如果有别的东西,我需要做的就是这个工作?谢谢

Can somebody point out if I'm doing something wrong or if there's something else I need to do to get this to work? Thanks

推荐答案

在默认情况下会不能在不同的应用程序之间共享。从我可以看到你有两个不同的应用程序程序App1 App2的这在不同的虚拟目录中运行,甚至可能单独的应用程序池,所以不要指望它们之间共享会话。

By default session cannot be shared between different applications. From what I can see you have two distinct applications App1 and App2 which run in separate virtual directories and probably even separate application pools, so don't expect to share session between them.

由于总是有变通,你会发现非常有用。正如你可以看到它使用一个黑客(反射),以规避ASP.NET团队设计师的决心不会暴露某些类别和属性,并让我们的生活因为开发商困难。

As always there are workarounds that you may find useful. As you can see it's using a hack (reflection) to circumvent ASP.NET team designer's determination to not expose certain classes and properties and make our life as developers difficult.

这篇关于为什么我不能分享2 Web应用程序使用的StateServer之间的会话状态?我在想什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆