473,503 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope of singleton static refs in ASP.NET 2.0

For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal static
reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page processing
?

In other words, will subsequent calls to getInstance() find uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null , and
re-instantiate an object of the class and assign it to unique instance ?
May 1 '06 #1
7 4221
Within the same appdomain the static variable will be set.

ASP.NET apps often however cross multiple worker process (as such multiple
app domains). If the next request came to a different worker process it
would use a different singleton instance.

Cheers,

Greg
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal static
reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page
processing ?

In other words, will subsequent calls to getInstance() find uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null , and
re-instantiate an object of the class and assign it to unique instance ?

May 1 '06 #2
Hi Greg, and thanks for the response.

By "next request came to a different worker process" are you referring to

(a) a call by another w3wp to the same method of the same singleton
data-access class that had previously been called by another w3wp in the
course of processing a specific page request

--or--

(b) during a subsequent page request by the same (or another) client, a call
is made to the same method of the same singleton data-access class

Thanks again.
"Greg Young" <Dr*************@hotmail.com> wrote in message
news:eh**************@TK2MSFTNGP04.phx.gbl...
Within the same appdomain the static variable will be set.

ASP.NET apps often however cross multiple worker process (as such multiple
app domains). If the next request came to a different worker process it
would use a different singleton instance.

Cheers,

Greg
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal
static reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page
processing ?

In other words, will subsequent calls to getInstance() find
uniqueInstance != null , and return it ? Or will they find
uniqueInstance == null , and re-instantiate an object of the class and
assign it to unique instance ?


May 1 '06 #3
A) ... in another process ... subsequent calls to the same appdomain will
reuse the same singleton. Also keep in mind that from time to time
applications recycle their worker processes which would also force the
singleton to be re-created (new process = new app domain)
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OX**************@TK2MSFTNGP05.phx.gbl...
Hi Greg, and thanks for the response.

By "next request came to a different worker process" are you referring to

(a) a call by another w3wp to the same method of the same singleton
data-access class that had previously been called by another w3wp in the
course of processing a specific page request

--or--

(b) during a subsequent page request by the same (or another) client, a
call is made to the same method of the same singleton data-access class

Thanks again.
"Greg Young" <Dr*************@hotmail.com> wrote in message
news:eh**************@TK2MSFTNGP04.phx.gbl...
Within the same appdomain the static variable will be set.

ASP.NET apps often however cross multiple worker process (as such
multiple app domains). If the next request came to a different worker
process it would use a different singleton instance.

Cheers,

Greg
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal
static reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page
processing ?

In other words, will subsequent calls to getInstance() find
uniqueInstance != null , and return it ? Or will they find
uniqueInstance == null , and re-instantiate an object of the class and
assign it to unique instance ?



May 1 '06 #4
John, are you storing your instance in HttpContext.Current? Or for
each page are you just doing:

Singleton sg = Singleton.Instance();

Where the Instance method just returns the instance created when the
Singleton class was initialized?

Thanks.

TJ

John A Grandy wrote:
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal static
reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page processing
?

In other words, will subsequent calls to getInstance() find uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null , and
re-instantiate an object of the class and assign it to unique instance ?


May 3 '06 #5
I haven't coded anything yet. I'm trying to determine if there is any
advantage to using singletons to improve performance of ASP.NET 2.0
web-apps.

From what I have learned so far, in almost any scenario there is no
advantage, because object instantiation is so cheap in .NET

In fact, there may even be a disadvantage to sharing singleton instances
across multiple page requests, due to multi-threading issues and locking.
This is the area I'm not sure about.

Right now, the area I'm concentrating is processing of a single page request
:

* Over the course of processing a single page request from a single client,
I believe that the advantages/disadvantages of storing class instance(s) in
some sort of cache (and removing them from the cache at end of page
processing) are only relevant if the class object requires a lot of
initialization.

That I know of , there are 4 types of cache the Page can easily use during
in-Page processing: HttpContext.Current.Application ,
HttpContext.Current.Session , or HttpContext.Current.Cache (Page.ViewState
could also be used ... but this would be a mis-use).

Assuming little or not initialization of the class object is required, it
seems that the overhead of retrieving the object from the cache is
approximately the same as the overhead of creating the object fresh.

* What confuses me about singletons in an ASP.NET web-app is the following:
Is it required to cache the singleton object ? True, the singleton has a
internal static reference to a single instance of itself , but, once
instantiated, does this assigned static reference protect the instance from
being disposed and GC'd once all other references to it have been dropped ?

For example: In a class method Class1.Method1() called during Page
processing , I perform dao = singletonDAO.getInstance() , make calls to
dao.method1() , dao.method2(), etc. ... once Class1.Method1() is completed,
is singletonDAO's internal static reference to an instance of itself
disposed (i.e. marked as ready for GC) ?

<tl*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
John, are you storing your instance in HttpContext.Current? Or for
each page are you just doing:

Singleton sg = Singleton.Instance();

Where the Instance method just returns the instance created when the
Singleton class was initialized?

Thanks.

TJ

John A Grandy wrote:
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal
static
reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page
processing
?

In other words, will subsequent calls to getInstance() find
uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null , and
re-instantiate an object of the class and assign it to unique instance ?

May 5 '06 #6
>What confuses me about singletons in an ASP.NET web-app is the following:
Is it required to cache the singleton object ?

You shouldn't need to cache the singleton if there is a static reference.
Assuming little or not initialization of the class object is required, it
seems that the overhead of retrieving the object from the cache is
approximately the same as the overhead of creating the object fresh. Cache retrieval is extremly fast and scalable as well. If your object
overhead is non-trivial, the cache approach is better.
is singletonDAO's internal static reference to an instance of itself
disposed (i.e. marked as ready for GC) ? No, static variables persist until the app domain unloads.
--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:O$**************@TK2MSFTNGP04.phx.gbl...I haven't coded anything yet. I'm trying to determine if there is any
advantage to using singletons to improve performance of ASP.NET 2.0
web-apps.

From what I have learned so far, in almost any scenario there is no
advantage, because object instantiation is so cheap in .NET

In fact, there may even be a disadvantage to sharing singleton instances
across multiple page requests, due to multi-threading issues and locking.
This is the area I'm not sure about.

Right now, the area I'm concentrating is processing of a single page
request :

* Over the course of processing a single page request from a single
client, I believe that the advantages/disadvantages of storing class
instance(s) in some sort of cache (and removing them from the cache at end
of page processing) are only relevant if the class object requires a lot
of initialization.

That I know of , there are 4 types of cache the Page can easily use during
in-Page processing: HttpContext.Current.Application ,
HttpContext.Current.Session , or HttpContext.Current.Cache
(Page.ViewState could also be used ... but this would be a mis-use).

Assuming little or not initialization of the class object is required, it
seems that the overhead of retrieving the object from the cache is
approximately the same as the overhead of creating the object fresh.

* What confuses me about singletons in an ASP.NET web-app is the
following: Is it required to cache the singleton object ? True, the
singleton has a internal static reference to a single instance of itself ,
but, once instantiated, does this assigned static reference protect the
instance from being disposed and GC'd once all other references to it have
been dropped ?

For example: In a class method Class1.Method1() called during Page
processing , I perform dao = singletonDAO.getInstance() , make calls to
dao.method1() , dao.method2(), etc. ... once Class1.Method1() is
completed, is singletonDAO's internal static reference to an instance of
itself disposed (i.e. marked as ready for GC) ?

<tl*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
John, are you storing your instance in HttpContext.Current? Or for
each page are you just doing:

Singleton sg = Singleton.Instance();

Where the Instance method just returns the instance created when the
Singleton class was initialized?

Thanks.

TJ

John A Grandy wrote:
For a singleton class utilizes by ASP.NET 2.0 page processing:

When initial instantiation is performed during the initial call to the
retrieve instance method (let's call the method "getInstance()"), an
instantiated object of the class is assigned to the class' internal
static
reference to that an object of itself (let's call the reference
"uniqueInstance")

Does that uniqueInstance have global scope in the context of page
processing
?

In other words, will subsequent calls to getInstance() find
uniqueInstance
!= null , and return it ? Or will they find uniqueInstance == null ,
and
re-instantiate an object of the class and assign it to unique instance ?


May 7 '06 #7
"Greg Young" <Dr*************@hotmail.com> wrote in message
news:Ou**************@TK2MSFTNGP02.phx.gbl...
A) ... in another process ... subsequent calls to the same appdomain will
reuse the same singleton. Also keep in mind that from time to time
applications recycle their worker processes which would also force the
singleton to be re-created (new process = new app domain)


Also ASP.NET can recycle the web app's AppDomain without recycling the whole
process which will have the same effect on the singleton

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
May 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2992
by: jbieger | last post by:
On my site I want to make two classes. One that can be instantiated normally and one with extended functionality that can only be instantiated once. The Singleton pattern thus seems like a logical...
3
3911
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
3
1745
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am...
2
1765
by: ks | last post by:
Hello, I'm kinda new to PHP and I'm running into something that just seems odd. There's a good chance I'm just doing something wrong though. I'm trying to implement a simple singleton: private...
5
1979
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private...
2
3512
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
26
2112
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
9
1972
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
8
1513
by: tech | last post by:
Is it ok to allow another object to own a singleton object, or is this definitely a NO NO. I have a utility class that i want to provide access to a whole group of subobjects so i can make this...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.