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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:35:06+00:00 2026-06-02T07:35:06+00:00

I want to store some data with Singleton class. As far as I’ve studied,

  • 0

I want to store some data with Singleton class. As far as I’ve studied, singleton class can be instantiated only for once. But it doesn’t work for me. Can someone correct my code:

public class MvcApplication : System.Web.HttpApplication
    {
        Singleton clientsessionidinstance = Singleton.GetInstance();
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "MVCPrj.Controllers" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            LOGICcLASSES.Logic.Auth ca = new LOGICcLASSES.Logic.Auth();
            clientsessionidinstance = Singleton.GetInstance();
            clientsessionidinstance.ClientSessionID = ca.Login(new LOGICcLASSES.Entities.ClientAuthentication()
            {
                IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
                UserName = ConfigurationManager.AppSettings["ClientUserName"],
                Password = ConfigurationManager.AppSettings["ClientPassword"]
            });
        }

        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Session != null)
            {
                if (!string.IsNullOrEmpty(clientsessionidinstance.ClientSessionID))
                {
                    CurrentUserSession.Store<string>("ClientSessionID", clientsessionidinstance.ClientSessionID);
                }
            }
        }
    }  

So the goal is this, @ Application_Start I have to log in an account. Then I should save the return string on HttpContext.Current.Session.
Unfortunately I can’t access the HttpContext.Current.Session inside Application_Start but it is possible on Application_AcquireRequestState.
I can use a variable that will hold the returned string and then use its value inside the Application_AcquireRequestState, but the big problem is this, the page loads? twice, so if I will use a variable, it will become null at the second load but Application_Start is still initiated once.
So I came up using a Singleton class, but still I get null values on second load.

Singleton class:

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

namespace MVCPrj.Areas.WebPage.Logic
{
    public class Singleton
    {
        private static Singleton instance;

        private Singleton() { }

        public static Singleton GetInstance()
        {
            lock (typeof(Singleton))
            {
                instance = new Singleton();
            }
            return instance;
        }

        private string _ClientSessionID;
        public string ClientSessionID
        {
            get { return _ClientSessionID; }
            set { _ClientSessionID = value; }
        }
    }
}  

EDIT

This code works for me after reading Singleton

public class Singleton
{
    private static readonly object _mutex = new object();
    private static volatile Singleton _instance = null;
    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_mutex)
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton();
                    }
                }
            }
            return _instance;
        }
    }

    private string _ClientSessionID;
    public string ClientSessionID
    {
        get { return _ClientSessionID; }
        set { _ClientSessionID = value; }
    }
}
  • 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-06-02T07:35:07+00:00Added an answer on June 2, 2026 at 7:35 am

    If you are using .NET4 the “perfect singleton” is often best achieved using System.Lazy.

    See also this web page for a description of many different singleton patterns in C# and their various pros and cons.

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

Sidebar

Related Questions

I have a Class that uses a dictionary to store some data. But I
I want to store some data from structures like this: class Project { ChildA
I want my application (PHP, but that shouldn't matter) to store some data in
I want to store some data during my site viewing. Sometime i need to
I want to temporary store some mapping data. The mapping is one to one.
I want to create a data store to allow me to store some data.
I have some data that I want to store somewhere in my Rails app
I want to store the average values of some data that is occasionally generated
I want to store some value from SQLite database in my buffer, but the
I want to store some data in the content provider, so some of my

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.