Static class instances unique to a request or 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; } } 

Using:

Page 1.aspx

 UserLoginSingleton.Instance.CreateUserObj(xml); 

Pase2.aspx:

UserLoginCollection userLoginCollection = UserLoginSingleton.Instance.getUserObj ();

The following is an article: link text

I set my collection object on page 1 and then I will do response.redirect or click on the link to go to page 2.aspx. However, my singleton instance does not have the collection object that I installed. How do I save my collection object through different pages for each session?

I know that static work does not work, as each instance will see the object, and I want this to be specific to each user.

+4
source share
5 answers

The HttpContext.Items collection is for every request. Therefore, in your case, when the user is redirected to page 2.aspx, the instance created on page 1 has disappeared. To make the same instance available to all requests, you need to use HttpContext.Session to store your instance.

+3
source

static fields are shared between requests. Stay tuned for standard multi-threaded issues!

HttpContext instances are not shared between requests.

0
source

Perhaps I need to rethink what I want to do. Basically my collection is a fo collection around different values ​​for a drop down list. This is being uploaded by the client and my webpage should display it.

I realized that the client can send it to me as serialized data, I gave them xsd and they will send it to me asynchronously during login. Page 1 will receive serialized data. The login page will register them. After page 1 receives the data, it will be deserialized into my strongly typed collection, which, in turn, will be used to create a drop-down list in page2.aspx

why am I doing this in such a primitive way. my hands are bound by restriction, and I try not to use session state to store this object, if I can avoid it.

Each user login comes with different drop-down options. user 1 caan has 10 parameters, and user 2 can have 200 options.

0
source

Just to indicate that your singleton implementation is incorrect, your singleton should be declared as follows

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

Use a static method that reads 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; } } } 
0
source

All Articles