Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 833583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:32:28+00:00 2026-05-15T04:32:28+00:00

Ok, I’ve got this singleton-like web class which uses session to maintain state. I

  • 0

Ok, I’ve got this singleton-like web class which uses session to maintain state. I initially thought I was going to have to manipulate the session variables on each “set” so that the new values were updated in the session. However I tried using it as-is, and somehow, it remembers state.

For example, if run this code on one page:

UserContext.Current.User.FirstName = "Micah";

And run this code in a different browser tab, FirstName is displayed correctly:

Response.Write(UserContext.Current.User.FirstName);

Can someone tell me (prove) how this data is getting persisted in the session? Here is the class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class UserContext
{
  private UserContext() { }

  public static UserContext Current
  {
    get
    {
      if (System.Web.HttpContext.Current.Session["UserContext"] == null)
      {
        UserContext uc = new UserContext();
        uc.User = new User();
        System.Web.HttpContext.Current.Session["UserContext"] = uc;
      }

      return (UserContext)System.Web.HttpContext.Current.Session["UserContext"];
    }
  }

  private string HospitalField;
  public string Hospital
  {
    get { return HospitalField; }
    set
    {
      HospitalField = value;
      ContractField = null;
      ModelType = null;
    }
  }

  private string ContractField;
  public string Contract
  {
    get { return ContractField; }
    set
    {
      ContractField = value;
      ModelType = string.Empty;
    }
  }

  private string ModelTypeField;
  public string ModelType
  {
    get { return ModelTypeField; }
    set { ModelTypeField = value; }
  }

  private User UserField;
  public User User
  {
    get { return UserField; }
    set { UserField = value; }
  }

  public void DoSomething()
  {
  }
}

public class User
{
  public int UserId { get; set; }
  public string FirstName { get; set; }
}

I added this to a watch, and can see that the session variable is definitely being set somewhere:

(UserContext)System.Web.HttpContext.Current.Session["UserContext"];

As soon as a setter is called the Session var is immediately updated:

    set
    {
      HospitalField = value; //<--- here
      ContractField = null;
      ModelType = null;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:32:29+00:00Added an answer on May 15, 2026 at 4:32 am

    The UserContextinstance is saved in Session with this line:

    System.Web.HttpContext.Current.Session["UserContext"] = uc;
    

    It’s not a singleton. The static property UserContext will attempt to retrieve a instance from Session, and if it doesn’t find it, create a new instance and store it in Session.

    UPDATE

    I can see how the session var is retrieved, my confusion is around how the session var is set.

    To add clarification following Micah’s comment: the first time the static Current property is accessed, a new UserContext instance is created, its User property is populated with a new User instance, and the UserContext instance is stored in Session. Subsequent accesses to UserContext.Current (and hence UserContext.Current.User) in the same session are all accessing the same instance.

    If it’s still not clear I suggest stepping through with a debugger.

    public static UserContext Current 
    { 
        get 
        { 
            // If Session does not yet contain a UserContext instance ...
            if (System.Web.HttpContext.Current.Session["UserContext"] == null) 
            { 
                // ... then create and initialize a new UserContext instance ...
                UserContext uc = new UserContext(); 
                uc.User = new User(); 
                // ... and store it in Session where it will be available for
                // subsequent requests during the same session.
                System.Web.HttpContext.Current.Session["UserContext"] = uc; 
            } 
            // By the time we get here, Session contains a UserContext instance,
            // so return it.
            return (UserContext)System.Web.HttpContext.Current.Session["UserContext"]; 
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have used the same exact method on a large… May 17, 2026 at 12:43 am
  • Editorial Team
    Editorial Team added an answer please look into the jobi's link for the slider previews… May 17, 2026 at 12:43 am
  • Editorial Team
    Editorial Team added an answer Use verbatim string by prefixing your string with @ string… May 17, 2026 at 12:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.