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

  • SEARCH
  • Home
  • 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 5952583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:42:08+00:00 2026-05-22T17:42:08+00:00

I’ve been reading about singletons in ASP.Net and I’ve seen various implementations and suggestions.

  • 0

I’ve been reading about singletons in ASP.Net and I’ve seen various implementations and suggestions. I’ve tried to model my implementation after this one: https://stackoverflow.com/…asp-net-singleton

Here is my question: I would like the object that I’m instantiating to last the life of the current session, but not be shared between sessions. For example, if two users are logged in at the same time, I want them to each “own” an instance of a global object. Below is my implementation. Is this the proper way to do this?

public class AppGlobal
{
    #region Contructors

    public AppGlobal() { }

    public static AppGlobal Instance
    {
        get
        {
            HttpSessionState session = HttpContext.Current.Session;

            if (session["AppGlobalInstance"] == null)
            {
                session["AppGlobalInstance"] = new AppGlobal();
            }

            return (AppGlobal)session["AppGlobalInstance"];
        }
    }

    #endregion

    #region Public Properties

    public User UserObject { get; set; }
    public Campaign CampaignObject { get; set; }
    public List<int> SelectedContactIDs = new List<int>();
    public List<int> UnsubmittedContactIDs = new List<int>();
    public List<int> SubmittedContactIDs = new List<int>();
    public List<int> ProcessedContactIDs = new List<int>();

    #endregion

    #region Public Instance Methods

    public void ClearCampaign()
    {
        CampaignObject = null;
        UnsubmittedContactIDs.Clear();
        SubmittedContactIDs.Clear();
        ProcessedContactIDs.Clear();
        SelectedContactIDs.Clear();
    }
    public void LoadCampaign(int campaignID)
    {
        //Ensure that old data is overwritten
        MailCampaignManagerEntities db = new MailCampaignManagerEntities();

        db.Campaigns.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;

        //Clear the campaign and associated data
        ClearCampaign();

        //Set campaign object in AppGlobal
        this.CampaignObject = db.Campaigns.SingleOrDefaultasp.net(x => x.CampaignID == campaignID);

        //Populate Contact Status Lists
        this.UnsubmittedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                                 where x.ContactSubmissionID == null
                                                 select x.CampaignContactID);

        this.SubmittedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                               where x.ContactSubmissionID != null
                                               select x.CampaignContactID);

        this.ProcessedContactIDs.AddRange(from x in this.CampaignObject.CampaignContacts
                                               where x.ContactSubmissionID != null
                                               && x.ContactSubmission.DateProcessed != null
                                               select x.CampaignContactID);
    }

    #endregion

    #region Public Static Methods

    public static void WriteLogEntry(int? campaignID, int? contactSubmissionID, int? scheduledDropID, int? userID, string activityDescription)
    {
        ActivityLog activityLog = new ActivityLog();
        activityLog.CampaignID = campaignID;
        activityLog.ContactSubmissionID = contactSubmissionID;
        activityLog.ScheduledDropID = scheduledDropID;
        activityLog.UserID = userID;
        activityLog.Text = activityDescription;
        activityLog.CreatedDate = DateTime.Now;

        using (MailCampaignManagerEntities db = new MailCampaignManagerEntities())
        {
            db.ActivityLogs.AddObject(activityLog);
            db.SaveChanges();
        }
    }

    #endregion
}
  • 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-22T17:42:09+00:00Added an answer on May 22, 2026 at 5:42 pm

    The implementation should usually be “okay”, but…

    You should mark the object as [Serializable], depending on the SessionStateModule that you have configured. Web farms or a web gardens typically use modules other than the InProc one, and they use serialization to store the session state. Otherwise, it looks okay for your object to be serialized, so no problems there.

    You may want to check if there is currently any session at all, or you could get a NullReferenceException. That would probably mean a misconfigured application however, or a call too early in the life cycle.

    Your application might allocate an AppGlobal object twice for a single session due to a race condition that you have in the way you check and set the Session variable. I don’t think even that is currently an issue, but it’s something to keep in mind if you want to include more fancy stuff. To prevent it, you can use lock like this:

    public class AppGlobal
    {
       private static object _syncRoot = new object();
    
       public static AppGlobal Instance
       {
           get
           {
               HttpSessionState session = HttpContext.Current.Session;
    
               lock (_syncRoot)
               {
                   if (session["AppGlobalInstance"] == null)
                   {
                       session["AppGlobalInstance"] = new AppGlobal();
                   }
               }
    
               return (AppGlobal)session["AppGlobalInstance"];
           }
        }     
    }
    

    If you ever want to store anything inside the object that prohibits serialization, and you need to support other SessionStateModules, you could store your instances in a collection that uses a classic Singleton pattern (here is a good implementation). ConcurrentDictionary would probably be a good one. As the key, you could use something unique that you do store in your Session, like a GUID. You would need to remove the entry from the collection when the session ends in any way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.