简体   繁体   中英

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

UserLoginSingleton.Instance.CreateUserObj(xml);

Pase2.aspx:

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. 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. So in your case when the user gets redirected to page2.aspx, the instance you created on page 1 is gone. For the same instance to be available across requests, you need to be using HttpContext.Session to store your instance.

static fields are shared between requests. Watch out for the standard multi-threaded issues!

HttpContext instances are not shared between requests.

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. Page 1 would receive the serialized data. 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

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.

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:


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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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