473,574 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Life of static variables in ASP.NET 2.0

I have a static variable defined something like this

private static Dictionary<stri ng, string[]> roles = new
Dictionary<stri ng,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 17814
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**********@c ommunity.nospam > wrote in message
news:uB******** ******@TK2MSFTN GP04.phx.gbl...
I have a static variable defined something like this

private static Dictionary<stri ng, string[]> roles = new
Dictionary<stri ng,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**********@c ommunity.nospam > wrote:
I have a static variable defined something like this

private static Dictionary<stri ng, string[]> roles = new
Dictionary<str ing,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***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP02.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.
"PeterKelln er" <pk**********@7 3rdstreet.com> wrote in message
news:pj******** *************** *********@4ax.c om...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@c ommunity.nospam > wrote:
I have a static variable defined something like this

private static Dictionary<stri ng, string[]> roles = new
Dictionary<st ring,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******** ********@TK2MSF TNGP03.phx.gbl. ..

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP02.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**********@c ommunity.nospam > wrote in message
news:uO******** ********@TK2MSF TNGP05.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.
"PeterKelln er" <pk**********@7 3rdstreet.com> wrote in message
news:pj******** *************** *********@4ax.c om...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@c ommunity.nospam > wrote:
I have a static variable defined something like this

private static Dictionary<stri ng, string[]> roles = new
Dictionary<s tring,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**********@h otmail.com> wrote in message
news:uO******** ******@TK2MSFTN GP05.phx.gbl...
In web this is not going to work

"Shimon Sim" <sh**********@c ommunity.nospam > wrote in message
news:uO******** ********@TK2MSF TNGP05.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.
"PeterKelln er" <pk**********@7 3rdstreet.com> wrote in message
news:pj******** *************** *********@4ax.c om...
On Wed, 17 May 2006 22:10:44 -0400, "Shimon Sim"
<sh**********@c ommunity.nospam > wrote:

I have a static variable defined something like this

private static Dictionary<stri ng, 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<stri ng, string[]> roles = new
Dictionary<stri ng,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

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

Similar topics

1
206
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 find out how static resources are being disposed in C#. The problem that I am having is I am opening to a file, write a text message and then close...
2
970
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
3919
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
5143
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
8
6795
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 code-behind and in business logic (C# classes used by the aspx) lose their value. I cannot reproduce this on our development server, so I cannot...
4
1739
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
6763
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 (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not...
9
8627
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the...
15
2956
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, so as not to clutter the global namespace with variables that are not needed elsewhere. For example, in Perl one can define a function foo like...
0
7764
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7863
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6514
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3776
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.