Singleton on multi server

Posted by Gauthamtech under C# on 11/1/2015 | Points: 10 | Views : 400 | Status : [Member] | Replies : 3
I am using below code for Implementing singleton instance and store some data in it as a list and access across class files in project. My question is what will happen if this project is hosted in two servers whether two instance will be created. Because i think it will create two instance if it is from two servers,That is what i need even. Code below Two questions: 1.How many instance if same server and multiple web site? 2.How many instance if two different server?

public class XXX {

private static XXX _instance;
// Lock synchronization object
private static readonly object SyncLock = new object();

private XXX()
{
}

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

public List<XXX> XXXDetails { get { return _XXX ; } set { _XXX = value; } }
private List<XXX> _XXX = new List<XXX>();
}




Responses

Posted by: Sheonarayan on: 11/1/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
1
Down
If this app is installed on two servers, it means two instance of the application will be running so two instance of the object will get created.

If multiple websites are using this app that is installed on single serer then single instance of this object will get created.

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Gauthamtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 11/1/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
1
Down
Yes it is two instance. Because when you are running the app on two instances it is nothing but different application but the same code.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Gauthamtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gauthamtech on: 11/1/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for your response .
Your first point is correct .
The second question is using singleton one application is build . That application is hosted in two instances of same server like app1 and app2 in ISS.
during that time whether the same instance will be used across the app1 and app2 or different instances?
I tried a POC with implementing the same I see two Instances created for app1 and app2 .
Can you confirm?

Gauthamtech, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response