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 6922695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:26:21+00:00 2026-05-27T10:26:21+00:00

Background/Question: I’m fairly new to the singleton design pattern. I’ve used it once in

  • 0

Background/Question:

I’m fairly new to the singleton design pattern. I’ve used it once in a web application (with the help of the SO community):

public static AppGlobal Instance
{
    get
    {
        if (HttpContext.Current.Session != null)
        {
            HttpSessionState session = HttpContext.Current.Session;

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

            return (AppGlobal)session["AppGlobalInstance"];
        }
        else
        {
            return null;
        }
    }
}

The above implementation makes sense to me because the instance of the AppGlobal is stored in the session. When the session dies, AppGlobal dies. What happens if I use the same design pattern in a class library that is called by a web application? For example, the users requests a page that calls methods in a DLL that doesn’t know about the session. Will the data stored in the singleton instance be persisted through multiple calls?

private static readonly Singleton instance = new Singleton();
private Singleton() { }

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

Additional Information:

Here’s what I’m trying to accomplish: I have a web application that is going to receive XML requests from a third party application. This XML will tell my web application to do one of three things (or all three of them). I would like to have a singleton instance of a class that stores data that can be accessed by multiple classes. I want the singleton instance to DIE after each request. If the above doesn’t accomplish this, what’s the best way to accomplish it?

Note: This web application runs on a single server and will never run on a farm.

EDIT 1:

Based on the suggestion below, I’ve used System.Web.HttpContext.Current.Session to store my class instance. Does this look like the correct approach for a singleton that will be unique to each session (remember I’m in a class library)?

    public static Ariba Instance
    {
        get
        {
            if (HttpContext.Current.Session != null)
            {
                HttpSessionState session = HttpContext.Current.Session;

                if (session["AribaInstance"] == null)
                {
                    session["AribaInstance"] = new Ariba();
                }

                return (Ariba)session["AribaInstance"];
            }
            else
            {
                return 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-27T10:26:22+00:00Added an answer on May 27, 2026 at 10:26 am

    It will be persisted through multiple calls, but there is one caveat. The static variables are scoped to the AppDomain, so any time the IIS worker process is recycled, any data stored in a static variable will be lost. The same is true of session data, if you’re storing it “in proc.”

    If you want an object that will only exist for the duration of the HTTP request, you can use the HttpContext.Items property.

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

Sidebar

Related Questions

Background to question: We are building an online web application that requires the user
So this question is regarding the .Net IAsyncResult design pattern and the necessity of
Edit 4: A new formatting for the question BACKGROUND: I have a class, Window,
(See this question and this question for background...) Given: grid = new Slick.Grid(#myGrid, data,
Here's the background for my question: My program will be getting a new copy
Background This question relates to the new Cirrus infrastructure for Aspect Oriented Programming in
Background/context As schema.org is relatively new, perhaps this question will promote more discussion than
Background (question further down) I've been Googling this back and forth reading RFCs and
Please refer to this background question. After constructing this COUNT, how would I then
Background This question is in two parts. I have a one-way WCF operation hosted

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.