473,385 Members | 2,243 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,385 software developers and data experts.

Maintaining state using static members

I'm relatively new to web services and I stumbled on some code that
maintains state between calls using a class with static members. This
is not the actual code, but you get the gist:

[WebMethod]
void AddToRepository(string name, int v)
{
ClassWIthStaticMember.AddToRepository(name, v);
}

[WebMethod]
int GetFromRepository(string name)
{
return ClassWIthStaticMember.GetFromRepository(name, v);
}

It seems to work, at least in the short term, but I'm concerned that
this is a very unreliable way of doing things. I'm not sure what the
lifetime of those static methods will be.

Any thoughts?

May 29 '06 #1
4 1958
You should read this article to get a better understanding of the lifetime
of static members in ASP.NET.

http://blogs.msdn.com/tess/archive/2...23/516139.aspx

In short, statics are not evil, but you have to understand that they don't
die until the AppDomain dies. In ASP.NET, the AppDomain can live for quite
a long time, allowing you to build up lots of references to whatever you are
stuffing into the static in the AddToRepository method.

--
Kirk Allen Evans
Developer Evangelist
Microsoft Corporation
blogs.msdn.com/kaevans

=== This post provided "AS-IS" with no warranties and confers no rights ===
<dm***********@gmail.com> wrote in message
news:11*********************@j55g2000cwa.googlegro ups.com...
I'm relatively new to web services and I stumbled on some code that
maintains state between calls using a class with static members. This
is not the actual code, but you get the gist:

[WebMethod]
void AddToRepository(string name, int v)
{
ClassWIthStaticMember.AddToRepository(name, v);
}

[WebMethod]
int GetFromRepository(string name)
{
return ClassWIthStaticMember.GetFromRepository(name, v);
}

It seems to work, at least in the short term, but I'm concerned that
this is a very unreliable way of doing things. I'm not sure what the
lifetime of those static methods will be.

Any thoughts?


May 29 '06 #2
Kirk, thanks for the reply.

My concern was that they will die too soon - not too late. So, is it
correct then that unless something disturbs the IIS AppDomain, such as
IISRESET or messing with one of the .config files, the static members
of classes accessed by the web service will live on?

May 29 '06 #3
Yep.

--
Kirk Allen Evans
Developer Evangelist
Microsoft Corporation
blogs.msdn.com/kaevans

=== This post provided "AS-IS" with no warranties and confers no rights ===
"Dmitri" <dm***********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Kirk, thanks for the reply.

My concern was that they will die too soon - not too late. So, is it
correct then that unless something disturbs the IIS AppDomain, such as
IISRESET or messing with one of the .config files, the static members
of classes accessed by the web service will live on?


May 30 '06 #4
Dmitri wrote:
Kirk, thanks for the reply.

My concern was that they will die too soon - not too late. So, is it
correct then that unless something disturbs the IIS AppDomain, such as
IISRESET or messing with one of the .config files, the static members
of classes accessed by the web service will live on?


The app domain runs under a worker process in IIS.

IIS 6 (Win2003) will recycle worker processes after a period (look at
hte properties on your application pool in IIS). Additionally, there
are other factors used to determine if a worker process should be
recycled: age, time spent idle, number of requests serviced, number of
requests queued, and amount of physical memory consumed.

Read: http://msdn.microsoft.com/msdnmag/is...PPipelines/#S2

--
Deepak Shenoy
http://shenoyatwork.blogspot.com
May 30 '06 #5

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

Similar topics

5
by: leegold2 | last post by:
Probably a newbie question about "state": My problem is I have a search form, so user enters a keyword <enter>, then this form posts to another page were the result are displayed. But this...
1
by: aiKeith | last post by:
Hello, I hate to admit it, but there have been times where I needed to persist a value in an asp.net app, and instead of using properties {get:set} I simply changed the variable to static -...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
1
by: Das | last post by:
Hi all, I'm using Asp.net. Within a form I have declared declared a public variable. When the form is loaded I set the property for the variable. but it doesn't maintain its state on postback. I...
8
by: Anthony P. Mancini | last post by:
I'm working on a proof of concept that will ultimately be deployed on a load balancer. For the sake of a preliminary demonstration I created a C# object and marked it's attributes as Public...
1
by: Novice | last post by:
Hey all, I have finally managed to create a Custom WebControl and am using a technique from another programmer to maintain state between pages - I would just like to validate this idea. ...
1
by: clintonG | last post by:
I'm having a problem maintaining state with a Panel control in a MasterPage and I need help thinking through this process. The basic structure of the HTML in the Master looks like this... ...
4
by: Sam | last post by:
I have an asp.net 2.0 app that uses a sitemap, Master Page, and has several content pages. While this feature has simplified the process of creating a data-driven site menu, it does seem to have...
3
by: Mahathi | last post by:
Hi I have a small problem in maintaining the state of a check box. Please do me a favour by telling me the procedure how to do that. My requirement is that "I have to map some roles with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.