how to Create Single instance of a class for all calls

S

sameer

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 ever i.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
 
R

rohit.shrivastava04

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.
 
F

Family Tree Mike

sameer said:
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 ever i.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

You could store the instance in an Application state variable, such as
Application ["MySingletonObject"] = mysingletonclass.GetInstance();. It may
also be possible that createing the single instance in the Application_Start
event is sufficient, but I have not tried that.

Mike
 
S

sameer

guys, thanks for you assistance but
1. this is not an Asp.net application so i am not making use of asp.net
objects.
2. this object is a TCPclient object which needs to maintain connection to
the tcp server on the other side.
3. using a webservice there is no gurantee that the object will be in memory
all the time, IIS can recycle that any time.

any other suggestions.

thanks
Sameer

Family Tree Mike said:
sameer said:
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 ever i.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

You could store the instance in an Application state variable, such as
Application ["MySingletonObject"] = mysingletonclass.GetInstance();. It may
also be possible that createing the single instance in the Application_Start
event is sufficient, but I have not tried that.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top