static variable values in web apps

S

Steven Nagy

Hi,

I ran a simple test today. I created a web app that calls a static
variable which self increments.
I then accessed the same app from another machine and the variable
increased, indicating the value is remembered across sessions, across
machines, etc

public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}

Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven
 
M

Mark Rae

I ran a simple test today. I created a web app that calls a static
variable which self increments.
I then accessed the same app from another machine and the variable
increased, indicating the value is remembered across sessions, across
machines, etc

That's right - static variables are created in the Application object,
meaning that there is only one instance of them across all Sessions...
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

If you want a variable which exists once for each session, place it in the
Session object.

If you want a variable which exists once for each request (OK, for each page
request), place it in the ViewState object.
 
G

Guest

Consider using a global/public class which can be initialized at the page
load and can be used anywhere in your code and dispose it at the request end.
 
H

Hans Kesting

Hi,
I ran a simple test today. I created a web app that calls a static
variable which self increments.
I then accessed the same app from another machine and the variable
increased, indicating the value is remembered across sessions, across
machines, etc
public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven

You can add values to the Context object:
Context.Items["key"] = somevalue;
or
System.Web.HttpContext.Current.Items["key"] = somevalue;

and retrieve it in a similar way.
This value will live for the duration of a single request!

Hans Kesting
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Steven said:
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

You could use the ThreadStatic attribute to create a static variable
that is local for the thread. I would advice against it, though, as the
value will only be initialised for the first thread. After that the
initial value will be undefined, but the compiler will not warn you if
you are using the undefined value. Also, the value will never go out of
scope, so you have to make sure to always set the value to null for the
garbage collection to work.

Why not just use a member variable in the class for the page? It's life
span is the same as the request and it's always initialised. It's not as
easily reachable from anywhere as you need a reference to the current
page object to reach the variable, but is that really a problem?
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top