I'm having an issue with the Singleton pattern.
It is really weird, but it looks like there are two or three instances of my Singleton Pattern. My website, is an actions site, with timers and I'm handling those timers and prices with my Singleton Object.
What is happening is that some people see some prices, and other people see other prices always when they are in different networks.
For example, In my office, my people see some auction at 0.56 cents, everybody sees the same, but in another network, for example, my house, I see 0.55 cents and also the timers have different values.
Having said this, I've tested my Singleton, by generating a GUID and logging it in my log file. Here is some code
public class Singleton
{
private static Singleton instance;
private static System.Threading.Mutex mutex;
System.Guid token;
private Singleton() {
token = System.Guid.NewGuid();
Logger.Log("New singleton Instance" + token.toString());
}
static Singleton()
{
instance = new Singleton();
mutex = new System.Threading.Mutex();
}
public static Singleton Acquire()
{
mutex.WaitOne();
return instance;
}
// Each call to Acquire() requires a call to Release()
public static void Release()
{
mutex.ReleaseMutex();
}
public void SomeAction()
{
Logger.Log(token.toString() + " - SomeAction");
}
}
In this code, I generate the token on the constructor, and log the creation of a new Singleton, then... in the SomeAction
method, I log who is doing that action.
After this, we made a couple of tests and downloaded the log file.
To my surprise, I see only one "New Singleton Instance bla" which is correct. but then, many calls to the SomeAction
Method with different GUIDs, which is weird.
I checked that object is only created in the static constructor, and I also checked that there is no manual creation anywhere.
For more information, this only happens on my production server, which is a goDaddy hosting. I've asked if there is more than one application pool for my website and they say there is only one app Pool.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
Your singleton lifetime is tied to the current worker process of IIS.
If you have multiple worker processes configured then not all of the requests are handled by the same process and thus not the same singleton.
I can't say for sure that this is your problem, but it might be worth checking:
Your web site may only be using one application pool, but an application pool may spawn more than one host process. Check the number of worker processes for your application pool.
The issue could be that your singleton pattern you have implemented is not thread safe. because in a web app you got more than one thread running at the same time(worker processes) it could happen that two thread create two different instance of your class. You should also consider where your web app is running on. If you have deployed your web app on a web-farm it could happen that different web server got a different instance of your class as a static variable "live" in the memory of the web server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With