c# stateserver 维护机器之间的会话
我确信我犯了一些我看不到的明显的错误。我希望你们中的一位能够纠正我的错误。
我的会话管理工作得很好,除了如果一台机器上的用户输入数据,在另一台机器上启动会话的用户也将从第一台机器上检索会话信息。不太好。 :(
我这样称呼我的会话:
UserInfo userinfo = UserInfo.Session;
我的会话管理类使用这个:
static UserInfo userInfo;
static public UserInfo Session
{
get
{
if (userInfo == null)
{
userInfo = new UserInfo();
userInfo.ResetSessionTime();
}
return userInfo;
}
}
我像这样读取和写入数据。我意识到我可以序列化整个类,但序列化和反序列化整个类似乎需要更多开销每次上课时,而不是只获取我需要的一两个项目,
Decimal _latitude;
private String SessionValue(String sKey, String sValue, String sNewValue)
{
String sRetVal = "";
if (sNewValue == null)//not wanting to update anything
{
if (sValue == null)//there is no existing value
{
sRetVal = (String)System.Web.HttpContext.Current.Session[sKey];
}
else
{
sRetVal = sValue;
}
}
else
{
System.Web.HttpContext.Current.Session[sKey] = sNewValue;
sRetVal = sNewValue;
}
return sRetVal;
}
public Decimal Latitude
{
get { return SessionValue("Latitude", _latitude); }
set { _latitude = SessionValue("Latitude", _latitude, value); }
}
感谢您的帮助。
I am sure that I have made some painfully obvious blunder(s) that I just cannot see. I am hoping one of you can set me straight.
I my session management is working perfectly except that if a user on one machine enters data, a user who starts a session on another machine will also retreive the session information from the first. Not so good. :(
I call my sessions like this:
UserInfo userinfo = UserInfo.Session;
My session mgt class uses this:
static UserInfo userInfo;
static public UserInfo Session
{
get
{
if (userInfo == null)
{
userInfo = new UserInfo();
userInfo.ResetSessionTime();
}
return userInfo;
}
}
I read and write the data like this. I realize that I could serialize the entire class, but it seems like a lot more overhead to serialize and deserialize an entire class each time the class is called as opposed to just grabbing the one or two items I need.
Decimal _latitude;
private String SessionValue(String sKey, String sValue, String sNewValue)
{
String sRetVal = "";
if (sNewValue == null)//not wanting to update anything
{
if (sValue == null)//there is no existing value
{
sRetVal = (String)System.Web.HttpContext.Current.Session[sKey];
}
else
{
sRetVal = sValue;
}
}
else
{
System.Web.HttpContext.Current.Session[sKey] = sNewValue;
sRetVal = sNewValue;
}
return sRetVal;
}
public Decimal Latitude
{
get { return SessionValue("Latitude", _latitude); }
set { _latitude = SessionValue("Latitude", _latitude, value); }
}
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 您正在为您的 UserInfo 使用静态数据,这意味着该类的单个实例在传入您的 Web 服务器的所有请求之间共享。
2)您不仅将值存储在会话中(不在用户之间共享),而且还存储在实例变量中,在这种情况下,实例变量将在用户之间共享。
所以 _latitude 的值导致了这个问题。一个简单的解决方案是这样的:
更好、更可测试的版本是:
在第二个实例中,在请求中您只需构造 HttpSessionStateWrapper (使用当前 Session)并将其传递给 UserInfo 实例。当你测试时,你可以传入一个模拟包装器。
无论如何,UserInfo 实例不应该在会话之间共享,它应该直接从会话中写入和读取。不要尝试通过保留会话值的本地版本来过早地优化事物。你没有节省任何时间,而且只是让自己容易受到错误的影响。
1) You're using statics for your UserInfo, which means that a single instance of this class is shared among all requests coming to your web server.
2) You're not only storing values in the session (which isn't shared among users) but also in an instance variable, which in this case WILL be shared among users.
So the value of _latitude is causing you this issue. A simple solution is this:
A better, more testable version would be:
In the second instance, within a request you just construct a new instance of the HttpSessionStateWrapper (using the current Session) and pass it to the UserInfo instance. When you test, you can just pass in a mock Wrapper.
No matter what, the UserInfo instance shouldn't be shared among sessions and it should write and read directly from the Session. Don't try to prematurely optimize things by keeping local versions of your session values. You aren't saving any time and you're just opening yourself up to bugs.
发生这种情况是因为您将用户信息存储在静态字段中。静态实例在所有请求之间共享,并在应用程序的整个生命周期内存在。
换句话说,您的所有用户都将从 UserInfo.Session 获得相同的 UserInfo 实例。
要解决此问题,您可以:
This happens because you store your user info in a static field. Static instances are shared between all requests, and lives the entire lifetime of your application.
In other words, all your users will get the same UserInfo instance from UserInfo.Session.
To fix this you could: