简体   繁体   English

ASP.NET中的请求或服务器特有的静态类实例?

[英]static class instances unique to a request or a server in ASP.NET?

 public sealed class UserLoginSingleton
{

     UserLoginCollection _userLoginCol = new UserLoginCollection();

    UserLoginSingleton()
    {
    }

    public static UserLoginSingleton Instance
    {
        get
        {
            IDictionary items = HttpContext.Current.Items;
            if (!items.Contains("TheInstance"))
            {
                items["TheInstance"] = new UserLoginSingleton();

            }
            return items["TheInstance"] as UserLoginSingleton;
        }
    }


    public void CreateUserObj(string xmlData)
    {
        _userLoginCol = (UserLoginCollection)_xmlUtil.Deserialize(xmlData, typeof(UserLoginCollection));
    }

    public UserLoginCollection getUserObj()
    {
        return _userLoginCol;
    }
}

Usage: 用法:

Page 1.aspx 第1.aspx页

UserLoginSingleton.Instance.CreateUserObj(xml);

Pase2.aspx: Pase2.aspx:

UserLoginCollection userLoginCollection = UserLoginSingleton.Instance.getUserObj(); UserLoginCollection userLoginCollection = UserLoginSingleton.Instance.getUserObj();

Followed the article here: link text 点击这里的文章: 链接文字

I set my collection object in page 1 and then do a response.redirect or click on link to get me to page 2.aspx. 我在第1页中设置了我的集合对象,然后执行response.redirect或单击链接以获取第2.aspx页。 However, my singleton instance has no collection object i set. 但是,我的单例实例没有设置的集合对象。 How do i persist my collection object across diff pages per each session? 如何在每个会话中跨多个页面保留我的集合对象?

I know static's wont work as every instance will see the object and i want that to specific per each user. 我知道静态不会工作,因为每个实例都会看到对象,我希望每个用户具体。

The HttpContext.Items collection is per-request. HttpContext.Items集合是按请求的。 So in your case when the user gets redirected to page2.aspx, the instance you created on page 1 is gone. 因此,在您将用户重定向到page2.aspx的情况下,您在第1页上创建的实例已消失。 For the same instance to be available across requests, you need to be using HttpContext.Session to store your instance. 要使相同的实例跨请求可用,您需要使用HttpContext.Session来存储您的实例。

static fields are shared between requests. 静态字段在请求之间共享。 Watch out for the standard multi-threaded issues! 注意标准的多线程问题!

HttpContext instances are not shared between requests. 请求之间不共享HttpContext实例。

Maybe I need to rethink what I want to do. 也许我需要重新思考我想做的事情。 Basically my collection is a collection fo around different values for a drop down list. 基本上我的集合是围绕下拉列表的不同值的集合。 This is fed by the client and my web page needs to display it. 这是由客户端提供的,我的网页需要显示它。

i figured the client could send it across to me as serialized data, i gave them the xsd, and they would send that to me asynchronously while logging on. 我想客户端可以将它作为序列化数据发送给我,我给了他们xsd,他们会在登录时异步发送给我。 Page 1 would receive the serialized data. 第1页将收到序列化数据。 the login page would log them in. once page1 has received the data, would deserialize into my strongly typed collection which in turn would be used to build a dropdown list in page2.aspx 登录页面将登录。一旦page1收到数据,将反序列化为我的强类型集合,反过来将用于在page2.aspx中构建下拉列表

why am i doing it in such a primitive way. 为什么我这样做是以原始方式进行的。 my hands are tied by restriction and i am trying not to use session state to hold this object if i can avoid it. 我的双手被限制捆绑,我试图不使用会话状态来保存这个对象,如果我可以避免它。

each user login comes in with various options for the dropdown list. 每个用户登录都有下拉列表的各种选项。 user 1 caan have 10 options and user 2 can have 200 options. user 1 caan有10个选项,user 2可以有200个选项。

Just to point out your singleton implementation is wrong, your singleton should be declared like this 只是要指出你的单例实现是错误的,你的单例应该像这样声明

public sealed class Singleton
{
    static readonly Singleton instance=new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

Use a static method that reads the HttpSession instead of a static variable: 使用读取HttpSession而不是静态变量的静态方法:


public class UserLoginController
{
    public static UserLoginController Instance
    {
        get
        {
            HttpSession session = HttpContext.Current.Session;
            if (session["UserLoginController"] == null)
            {
                session["UserLoginController"] = new UserLoginController();
            }
            return session["UserLoginController"] as UserLoginController;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM