How does static objects behave in ASP.NET

M

Michael

Hi,

I have a class with static fields. The values of those fields appear to
stick between Web requests. That behaviour is a bit of a surprise to me, as
I would expect the object to be completely destroyed between Web requests
(maybe not technically correct way of describing this, since the fields are
static, there is no object instantiation).

The static fields values do not seem to be shared between sessions though;
that's good.

Any additional info, or pointers to where I can read more about this on the
Net would be much welcomed.

Thanks.
 
S

Stefan

Uhm, why would a static field be destroyed? It is created when the
_type_ (not an object/instance of this type) is loaded and destroyed.

Also, they will be shared between session (they are similar to
application scoped variables), so I think you should retest that.
 
S

Scott Allen

I have a few articles on the topic:
You'll find those statics fields are shared across sessions, in fact
each web application will have only one instance of each static field,
so you have to be very careful how you use them in a web app.

I have a few articles on the topic:

Get a Charge From Statics with Seven Essential Programming Tips
http://msdn.microsoft.com/msdnmag/issues/05/01/StaticsinNET/default.aspx

Statics & Thread Safety: Part I
http://odetocode.com/Articles/313.aspx

Statics & Thread Safety: Part II
http://odetocode.com/Articles/314.aspx
 
M

Michael

Thanks for the info and links.


Scott Allen said:
I have a few articles on the topic:
You'll find those statics fields are shared across sessions, in fact
each web application will have only one instance of each static field,
so you have to be very careful how you use them in a web app.

I have a few articles on the topic:

Get a Charge From Statics with Seven Essential Programming Tips
http://msdn.microsoft.com/msdnmag/issues/05/01/StaticsinNET/default.aspx

Statics & Thread Safety: Part I
http://odetocode.com/Articles/313.aspx

Statics & Thread Safety: Part II
http://odetocode.com/Articles/314.aspx
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top