Singleton Problems

Posted on 2005-04-26
Last Modified: 2010-04-16
I have an issue with singleton implementation in C#, my business object has a 3 hash tables this class follows a singleton pattern. I have the following test scenario.I have a test.aspx which populates the hash tables with the data from the database.I have an exe which also instantiates this class to view the refreshed data.I run the aspx first and then run the exe, when the exe is run it says the instance is already present and returns the instance but that instance doesnot have any data in the hash table. My issue is that the singleton is creating a fresh instance or cleaning the data in the hashtable everytime its called from a client could the C# experts throw some light on this.
Question by:vinyel23
    LVL 7

    Accepted Solution

    One if the issues that I ran into with the singleton is that it is bound by application domain.  Basically, each exe creates a seperate application domain and therefor (in my experience) can create a new singleton.  The way around this is to use remoting to cross the application domains.  I chose to figure out a different solution at this point but that is what I recall when I ran into a similiar issue.

    LVL 37

    Expert Comment

    the aspx and the exe are not using the same singleton as Tuck points out above ...

    A centralized source will be required, there are a few methods of doing this ...

    1) Web Service
    2) Singleton Remoting
    3) File Based Persistance
    4) DB Based Persistance
    5) Shared Memory Based Persistance

    naturally each has its positives and negatives .. could you let us know a bit more about your requirements?




    Author Comment

    Thanks tuck your comment rang a bell i changed my app domain to and the singleton works like a champ...
    Thanks again
    LVL 37

    Expert Comment

    beware: if an assembly is unloaded so is your singleton ... does this from time to time (unload a domain) depending on your setup.

    also the use of this causes problems because you force yourself into a single concurrent app pool ... i.e. you can only process 1 concurrent request.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now