473,287 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Life of static variables in ASP.NET 2.0

I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the data
in this variable.

Thank you.
May 18 '06 #1
14 17797
Yes, and that isn't necessarily a good thing. Static entities are not
necessarily thread-safe. Be careful about when you use them.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.

"Shimon Sim" <sh**********@community.nospam> wrote in message
news:uB**************@TK2MSFTNGP04.phx.gbl...
I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the
data in this variable.

Thank you.

May 18 '06 #2
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@community.nospam> wrote:
I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the data
in this variable.

Thank you.


Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.

So, I'm assuming you don't want to go after the data again so you
should either store it in your viewstate, Session or Cache.

One big warning that Scott Guthrie gave me once though is be very
careful when persisting security data (like role information). It can
cause some ugly security scenarios.

Good luck,

-Peter

PS: nice Dictionary Declaration! I love generics.
Peter Kellner
http://peterkellner.net
May 18 '06 #3
CS

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
that isn't necessarily a good thing.
Why ?
Static entities are not necessarily thread-safe. Be careful about when you
use them.


I say it is safe. Because it is "static". Static means "fixed", not
modifiable. Otherwise,
it is dynamic, which may not be safe. Static data only get changed when
ASP.net
recycles.

Example: Static data may be loaded in a singleton class. In this case,
whomever
references the variable 1st will load the class. The class will stay there
until Asp.Net recycles
and or web server service is restarted.

Comacho
May 18 '06 #4
I have this variable in a class
public class UserVerifier //just made up a name
{
private static roles=...
}
And then I am calling it from HttpModule for each request.
I have some issues with my app and I want to make sure that everything I did
is 100%.
Thanks.
"PeterKellner" <pk**********@73rdstreet.com> wrote in message
news:pj********************************@4ax.com...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@community.nospam> wrote:
I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the
data
in this variable.

Thank you.


Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.

So, I'm assuming you don't want to go after the data again so you
should either store it in your viewstate, Session or Cache.

One big warning that Scott Guthrie gave me once though is be very
careful when persisting security data (like role information). It can
cause some ugly security scenarios.

Good luck,

-Peter

PS: nice Dictionary Declaration! I love generics.
Peter Kellner
http://peterkellner.net

May 18 '06 #5
Dictionary description on MSND does say that iteration operations are not
thread safe.
Shimon.
"CS" <CS@HotMeal.Com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
that isn't necessarily a good thing.


Why ?
Static entities are not necessarily thread-safe. Be careful about when
you use them.


I say it is safe. Because it is "static". Static means "fixed", not
modifiable. Otherwise,
it is dynamic, which may not be safe. Static data only get changed when
ASP.net
recycles.

Example: Static data may be loaded in a singleton class. In this case,
whomever
references the variable 1st will load the class. The class will stay there
until Asp.Net recycles
and or web server service is restarted.

Comacho

May 18 '06 #6
In web this is not going to work

"Shimon Sim" <sh**********@community.nospam> wrote in message
news:uO****************@TK2MSFTNGP05.phx.gbl...
I have this variable in a class
public class UserVerifier //just made up a name
{
private static roles=...
}
And then I am calling it from HttpModule for each request.
I have some issues with my app and I want to make sure that everything I
did is 100%.
Thanks.
"PeterKellner" <pk**********@73rdstreet.com> wrote in message
news:pj********************************@4ax.com...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@community.nospam> wrote:
I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the
data
in this variable.

Thank you.


Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.

So, I'm assuming you don't want to go after the data again so you
should either store it in your viewstate, Session or Cache.

One big warning that Scott Guthrie gave me once though is be very
careful when persisting security data (like role information). It can
cause some ugly security scenarios.

Good luck,

-Peter

PS: nice Dictionary Declaration! I love generics.
Peter Kellner
http://peterkellner.net


May 18 '06 #7
Usually it is working here.
Do you have any specifics why not?
Thanks

"MSDN" <sq**********@hotmail.com> wrote in message
news:uO**************@TK2MSFTNGP05.phx.gbl...
In web this is not going to work

"Shimon Sim" <sh**********@community.nospam> wrote in message
news:uO****************@TK2MSFTNGP05.phx.gbl...
I have this variable in a class
public class UserVerifier //just made up a name
{
private static roles=...
}
And then I am calling it from HttpModule for each request.
I have some issues with my app and I want to make sure that everything I
did is 100%.
Thanks.
"PeterKellner" <pk**********@73rdstreet.com> wrote in message
news:pj********************************@4ax.com...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@community.nospam> wrote:

I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the
data
in this variable.

Thank you.
Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.

So, I'm assuming you don't want to go after the data again so you
should either store it in your viewstate, Session or Cache.

One big warning that Scott Guthrie gave me once though is be very
careful when persisting security data (like role information). It can
cause some ugly security scenarios.

Good luck,

-Peter

PS: nice Dictionary Declaration! I love generics.
Peter Kellner
http://peterkellner.net



May 18 '06 #8
> I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the data
in this variable.

Thank you.


That same "roles" Dictionary will be available to all users (Sessions)
of your application, until the application recycles. The data in it
could be modified by any thread accessing it, so there you will need
some locking mechanism.

Hans Kesting
May 18 '06 #9
Hi Shimon,

As for static variables, they'll live during the lifetime of its host
AppDomain. And as for ASP.NET application, if you're not using webgarden or
webfarm to load balacne it, each application instance will be hosted in a
single AppDomain in the worker process. The static variables will be
available during the application's lifetime.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 18 '06 #10
> I say it is safe. Because it is "static". Static means "fixed", not
modifiable. Otherwise,
it is dynamic, which may not be safe. Static data only get changed when
ASP.net
recycles.
You can say anything you want. You can believe anything you want. As far as
the facts are concerned, it doesn't make a lick of difference. Facts are not
matters of opinion, nor does opinion affect facts. "Static" does not mean
"fixed." A static member or class is a single-instance entity that is not
instantiable is scoped to an application. It can certainly be changed. If
you have a static integer, for example, you can assign any value to it.

As to thread-safety, you may (or may not) want to read the following MSDN
Magazine article:

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

It actually contains a lot more information about statics than thread
issues, but discusses that as well.

The following resources may also be of use:

http://msdn.microsoft.com/library/de...rfstaticpg.asp
http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.

"CS" <CS@HotMeal.Com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
that isn't necessarily a good thing.


Why ?
Static entities are not necessarily thread-safe. Be careful about when
you use them.


I say it is safe. Because it is "static". Static means "fixed", not
modifiable. Otherwise,
it is dynamic, which may not be safe. Static data only get changed when
ASP.net
recycles.

Example: Static data may be loaded in a singleton class. In this case,
whomever
references the variable 1st will load the class. The class will stay there
until Asp.Net recycles
and or web server service is restarted.

Comacho

May 18 '06 #11
> Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.
I hope you're not referring to static entities here. They exist for the
lifetime of the application. The application lifetime extends well beyond
the scope of a single instance of a Page.

ASP.Net is not stateless. HTTP is a stateless protocol. An ASP.Net Page
instance lasts for the time it takes to respond to a single Request. The
application lasts from the first Page Request from any client until 20
minutes after the last Page Request from any client.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"PeterKellner" <pk**********@73rdstreet.com> wrote in message
news:pj********************************@4ax.com... On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@community.nospam> wrote:
I have a static variable defined something like this

private static Dictionary<string, string[]> roles = new
Dictionary<string,string[]>();

Can I safely assume that it will be live for the live of application?

Meaning that any thread/page of the application will have access to the
data
in this variable.

Thank you.


Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.

So, I'm assuming you don't want to go after the data again so you
should either store it in your viewstate, Session or Cache.

One big warning that Scott Guthrie gave me once though is be very
careful when persisting security data (like role information). It can
cause some ugly security scenarios.

Good luck,

-Peter

PS: nice Dictionary Declaration! I love generics.
Peter Kellner
http://peterkellner.net

May 18 '06 #12
Interesting remark on the thread-safety of iteration; what exactly does
that mean in the context of:

static ICollection<T> myCollection;

....

foreach(T member in myCollection)
{
doSomethingWithMember(...);
}

Will each foreach have a separate iterator? Is the thread-safety issue
simply due to values being changed/appended or are there subtler
problems?

Is it possible to have a readonly iterator on a collection in .Net like
the T::const_iterator in C++, and would a collection accessible only by
such an iterator be thread safe?

May 18 '06 #13
On Thu, 18 May 2006 06:09:53 -0400, "Kevin Spencer"
<ke***@DIESPAMMERSDIEtakempis.com> wrote:
Hmmm. You are asking this in the asp.net forum which makes me think
you are thinking the "live of the application" is more than one page
hit. I'll go on a little assuming this is the case, otherwise,
forgive me for rambling.

Since asp.net is stateless, nothing you declare in your local stack or
heap will persist through a postback.


I hope you're not referring to static entities here. They exist for the
lifetime of the application. The application lifetime extends well beyond
the scope of a single instance of a Page.

ASP.Net is not stateless. HTTP is a stateless protocol. An ASP.Net Page
instance lasts for the time it takes to respond to a single Request. The
application lasts from the first Page Request from any client until 20
minutes after the last Page Request from any client.

Good point. thanks for correcting me.
Peter Kellner
http://peterkellner.net
May 18 '06 #14
Static members are accessible across classes, instances, and threads.
Because .Net is multi-threaded, you can run into a situation where 2 or more
threads are trying to access and/or change a static variable at the same
time, with unexpected consequences. Static members should therefore be
locked when accessing. The following is a good article that provides a
number of helpful threading "rules" -

http://www.anticipatingminds.com/Con...ledgePack.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"Flinky Wisty Pomm" <Pa********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Interesting remark on the thread-safety of iteration; what exactly does
that mean in the context of:

static ICollection<T> myCollection;

...

foreach(T member in myCollection)
{
doSomethingWithMember(...);
}

Will each foreach have a separate iterator? Is the thread-safety issue
simply due to values being changed/appended or are there subtler
problems?

Is it possible to have a readonly iterator on a collection in .Net like
the T::const_iterator in C++, and would a collection accessible only by
such an iterator be thread safe?

May 18 '06 #15

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

Similar topics

1
by: Natalia DeBow | last post by:
Hi, I have a quick question. I am using a static variable to hold an instance of the StreamWriter: private static StreamWriter sw = File.AppendText("C:\\Temp\\Test.txt"); I am curious to...
2
by: Eirik WS | last post by:
What's the use in static variables, like 'static int INTEGER' or 'static char CHARACTER'? -- No matches found
9
by: ur8x | last post by:
Hi, I was wondering if there is a difference between declaring a static variable outside of the method as oppose to declaring it as a local variable? Thanks.
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
4
by: David Buck | last post by:
If I have the following setup void Vdlog_SetVport(int reason) { static double lx,ly,hx,hy; Vmath_GetBox(&lx,&ly,&hx,&hy); .... }
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
15
by: kj | last post by:
Yet another noob question... Is there a way to mimic C's static variables in Python? Or something like it? The idea is to equip a given function with a set of constants that belong only to it,...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.