PC Review


Reply
Thread Tools Rate Thread

how to Create Single instance of a class for all calls

 
 
sameer
Guest
Posts: n/a
 
      28th May 2009
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
 
Reply With Quote
 
 
 
 
rohit.shrivastava04@gmail.com
Guest
Posts: n/a
 
      28th May 2009
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.



 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      28th May 2009


"sameer" 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 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
 
Reply With Quote
 
sameer
Guest
Posts: n/a
 
      28th May 2009
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" wrote:

>
>
> "sameer" 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 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databind to a single instance of a class moondaddy Microsoft Dot NET Framework Forms 1 28th Mar 2006 08:14 PM
Ensure single class instance but with multiple references to it hooksie2@hotmail.com Microsoft Excel Programming 4 6th Jan 2006 02:31 AM
How to have New() in a Base Class create and return an instance of a Derived Class? Joe HM Microsoft VB .NET 4 22nd Nov 2005 09:31 PM
single instance base class =?Utf-8?B?bWlrZSB3Lg==?= Microsoft VB .NET 1 26th Oct 2004 06:19 PM
Activator could not create a instance of the class as the Length of the class name is 36 characters Jack Wright Microsoft C# .NET 0 18th Mar 2004 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:26 PM.