On May 28, 9:38*am, sameer <sam...@discussions.microsoft.com> wrote:
> guys,
>
> envirnment : C#, VS 2008, .Net 3.5 framework
>
> i am trying to create a single instance of a class within a dll, this class
> would be called both from a webservice application as well as invoked locally
> from an exe on the same machine. the requriement is to make sure that only
> one instance of this class is created
> * * * * a. * * *no matter how many time this class is initialized
> * * * * b. * * *no matter if it is locally or remotely
> * * * * there cannot bemore then one instance of this object everi.e only one
> instance would server all the calls.
>
> using singleton pattern below is the code that i wrote :
>
> public class mysingletonclass
> * * {
>
> * * * * private static volatile mysingletonclass _Instance = null;
>
> * * * * public static mysingletonclass GetInstance()
> * * * * {
> * * * * * * if (_Instance == null)
> * * * * * * {
> * * * * * * * * lock (_Lock)
> * * * * * * * * {
> * * * * * * * * * * if (_Instance == null)
> * * * * * * * * * * {
>
> * * * * * * * * * * * * _Instance = new mysingletonclass ();
> * * * * * * * * * * }
> * * * * * * * * }
>
> * * * * * * }
>
> * * * * * * return _Instance;
> * * * * }
>
> but when seems that it creates an instace for every application that invokes
> it, i.e if the webserice calls it , for all the calls it uses the same
> instance where as if an exe invokes it it creates a new instance then use
> that same one, again my reuqirement is to make sure that only one instance of
> this class exists on the machine level and it is served to all client.
>
> please suggest!
>
> thanks
>
> sameer
Live Object i.e. instance of a class, can only be alive in a process.
Same like human being can only be live on earth. So earth is a process
which contains our instances. Same like this all instances are
instantiated in a process. So following singleton will disallow
second object creation in a process only.
To acheive your requirement, I would suggest, First host your dll in a
web service or use remoting. In that case you will be having one
instance in the web service process and then do a remote call/ web
service call from windows exe or from other webservices.
I guess this would help you.
|