我遇到了Singleton模式的问题.
这真的很奇怪,但看起来我的Singleton模式有两三个实例.我的网站,是一个动作网站,有计时器,我正在使用我的Singleton对象处理这些计时器和价格.
发生的事情是有些人看到一些价格,而其他人总是在不同的网络中看到其他价格.
例如,在我的办公室,我的人看到一些0.56美分的拍卖,每个人都看到相同的,但在另一个网络,例如,我的房子,我看到0.55美分,而计时器有不同的价值.
话虽如此,我已经通过生成GUID并将其记录在我的日志文件中来测试我的Singleton.这是一些代码
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");
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我在构造函数上生成令牌,并记录新Singleton的创建,然后在SomeAction方法中记录谁在执行该操作.
在此之后,我们进行了几次测试并下载了日志文件.
令我惊讶的是,我只看到一个"New Singleton Instance bla",这是正确的.但是,很多人SomeAction使用不同的GUID 调用Method,这很奇怪.
我检查过该对象只在静态构造函数中创建,我还检查过在任何地方都没有手动创建.
有关更多信息,这只发生在我的生产服务器上,这是一个goDaddy托管.我问过我的网站是否有多个应用程序池,他们说只有一个应用程序池.