Singleton Problems

Posted on 2005-04-26
Medium Priority
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
  • 2

Accepted Solution

NipNFriar_Tuck earned 1000 total points
ID: 13870562
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

ID: 13872152
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

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

Expert Comment

ID: 13872884
beware: if an assembly is unloaded so is your singleton ... asp.net 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 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