472,993 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 1943
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.