c# stateserver 维护机器之间的会话

发布于 2024-08-13 04:27:51 字数 1404 浏览 3 评论 0原文

我确信我犯了一些我看不到的明显的错误。我希望你们中的一位能够纠正我的错误。

我的会话管理工作得很好,除了如果一台机器上的用户输入数据,在另一台机器上启动会话的用户也将从第一台机器上检索会话信息。不太好。 :(

我这样称呼我的会话:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-08-20 04:27:51

1) 您正在为您的 UserInfo 使用静态数据,这意味着该类的单个实例在传入您的 Web 服务器的所有请求之间共享。

2)您不仅将值存储在会话中(不在用户之间共享),而且还存储在实例变量中,在这种情况下,实例变量将在用户之间共享。

所以 _latitude 的值导致了这个问题。一个简单的解决方案是这样的:

public class Userinfo
{
    public Decimal Latitude
    {
        get { return System.Web.HttpContext.Current.Session["Latitude"]; }
        set { System.Web.HttpContext.Current.Session["Latitude"] = value; }
    }
}

更好、更可测试的版本是:

public class UserInfo
{
    private HttpSessionStateWrapper _session;
    public UserInfo(HttpSessionStateWrapper session)
    ( 
       // throw if null etc
       _session = session;
    )

    public Decimal Latitude
    {
        get { return _session["Latitude"]; }
        set { _session["Latitude"] = value; }
    }
}

在第二个实例中,在请求中您只需构造 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:

public class Userinfo
{
    public Decimal Latitude
    {
        get { return System.Web.HttpContext.Current.Session["Latitude"]; }
        set { System.Web.HttpContext.Current.Session["Latitude"] = value; }
    }
}

A better, more testable version would be:

public class UserInfo
{
    private HttpSessionStateWrapper _session;
    public UserInfo(HttpSessionStateWrapper session)
    ( 
       // throw if null etc
       _session = session;
    )

    public Decimal Latitude
    {
        get { return _session["Latitude"]; }
        set { _session["Latitude"] = value; }
    }
}

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.

野心澎湃 2024-08-20 04:27:51

发生这种情况是因为您将用户信息存储在静态字段中。静态实例在所有请求之间共享,并在应用程序的整个生命周期内存在。

换句话说,您的所有用户都将从 UserInfo.Session 获得相同的 UserInfo 实例。

要解决此问题,您可以:

  • 将整个类序列化到会话中。我不知道你还有哪些其他属性,但我猜这不会有太多开销。
  • 每个请求创建一个 UserInfo 实例,以便用户始终从新实例读取,这反过来又会刷新 Session 中的值。

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:

  • Serialize the whole class into session. I don't know which other properties you have, but I would guess it would not be too much of an overhead.
  • Create an instance of UserInfo per request, so that the user always reads from a new instance, which in turn will refresh it's values from Session.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文