473,396 Members | 1,987 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,396 software developers and data experts.

static variable lifetime in WebService

Hi,

I have webservice which holds a static variable.
I send my first message into the webservice and the static variable gets
a value. If I queried the webservice at a later time (say after 1 hour)
would the static variable still hold the same value, or would the code
be re-initialised and the static variable back to the default value:

//static variable is default 0
//Call webservice
//static variable is set to 1
//Call webservice 1 hour later
//What value is static variable?

My understanding of the webservice process space (in terms of static
vars) was that it matched a windows service, where a static variable
value persisted for the lifetime of the service.

Thanks in advance.

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #1
19 23318
On Jan 10, 10:04 am, Steven Blair <steven.bl...@btinternet.comwrote:

<snip>
My understanding of the webservice process space (in terms of static
vars) was that it matched a windows service, where a static variable
value persisted for the lifetime of the service.
It's persisted for the lifetime of the AppDomain. However, ASP.NET may
recycle AppDomains for various reasons, at which point the value of
your variable will be lost.

Jon
Jan 10 '08 #2
Jon

Thanks, I have now turned off the recycling in IIS and seems to be
working fine now.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #3
Just remember that IIS recycling is there for a reason... if you need
persisted state, then perhaps consider using a database? Or
alternatively host via a windows service, not IIS. The latter is easy
to do for WCF, but a little tricker for an "asmx" style web-service.

But a database would be my first instinct...

Marc
Jan 10 '08 #4
As Marc indicated, just "turning off recycling" in IIS is not a very good
guarantee that your app won't lose a static variable's value. If this is the
approach, you may want to modify the Application_Start handler in global.asax
to restore the value on a restart, or use a database or other reliable
persistence mechanism as was suggested.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Steven Blair" wrote:
Jon

Thanks, I have now turned off the recycling in IIS and seems to be
working fine now.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #5
Well from what I see this is exactly what I want. The application really
does need to be a webservice (for a number of reasons) and I just need
to preserve the application at all times.

What concerns would you have by this approach. Is there soemthing I have
missed?

The overhead of the AppDomain surely its negligible (its a very simple
webservice with a few statics that on each call, creates an instance of
a dll, does some work (consults the static variables) and finishes.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #6
Steven Blair <st**********@btinternet.comwrote:
Well from what I see this is exactly what I want. The application really
does need to be a webservice (for a number of reasons) and I just need
to preserve the application at all times.

What concerns would you have by this approach. Is there soemthing I have
missed?

The overhead of the AppDomain surely its negligible (its a very simple
webservice with a few statics that on each call, creates an instance of
a dll, does some work (consults the static variables) and finishes.
1) If your server falls over (e.g. power cut) you've got problems if
you've got no more persistence than memory

2) Changes to the application or its configuration will require
AppDomain recycling

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 10 '08 #7
well, reboot would be the obvious one...

But also; if your state management is in memory, you have no
opportunity to load-balance / failover. For me, that would be a big
problem. A database (especially on a dedicated server) provides an
abstraction to this, allowing all your servers to share state, and
allowing you to change the cluster on the fly without affecting the
data that people see. Maybe you don't need this level of complexity.

Also - the fact is that occasionally apps die, and it would be
simplistic to assume that the single appdomain is going to run
indefinitely within IIS. A vanilla windows service (since it is much
simpler) will probably run for much longer, but eventually (either due
to an OS restart, or just karma) it will have to be restarted. State
gone.

Marc
Jan 10 '08 #8
Rebooting / crashing is something we considered so we have a primary,
secondary and a tertiary site for such events.
If the box is down, communication is moved over to secondary etc.
If a next communcation came in after a box was rebooted therefore still
accessed the primary, the statics would be lost, but we have a process
in place for this scenario.

Thanks for the advice, and perhaps the next time I appraoch a similar
problem a Database might be considered.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #9
Rebooting / crashing is something we considered so we have a primary,
secondary and a tertiary site for such events.
If the box is down, communication is moved over to secondary etc.
If a next communcation came in after a box was rebooted therefore still
accessed the primary, the statics would be lost, but we have a process
in place for this scenario.

Thanks for the advice, and perhaps the next time I appraoch a similar
problem a Database might be considered.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '08 #10
Ok, so it seems I do have a problem with this approach.
Each call to my webservice creates an instance of a dll but I don't
think this dll is ever cleaned up (memory wise).
Is the dll still managed memory in this scenario, or have I turned of
memory allocation with the IIS settings and the number of instance keeps
pilling up?

If I stop IIS, I get lots of lines of trace that I added to a destructor
(just for debug purposes) showing the class being destroyed.

Assuming this is the case, the only way this memory can be released is
by stopping the service or by activating the recycling again?
*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '08 #11
Each call to my webservice creates an instance of a dll
Do you really mean dll? How do you mean? Do you mean your web-service
loads a few other .NET assemblies? In which case, they will only get
loaded once per AppDomain (not over and over); but an assembly will
not (and cannot) be unloaded without tearing down the AppDomain.
If you mean object instances, then that shouldn't be a problem; if you
are seeing leak-like behavior, then verify you haven't added your
objects to something longer-lived... event subscriptions (especially
static/singleton etc subscriptions) are typical offenders here, but
other things (collections etc) are possible.

If you mean something more esoteric, you'll have to add detail to give
a meaningful reply...

It would also help if you indicated how you were monitoring the
memory; simple measures (taskman, for instance) can be deceptive...

Marc
Jan 11 '08 #12
One other thing... if you are using *static* variables, you probably
want to be very careful about thread synchronisation; but this may
effectively serialize access to your service (i.e. one at a time,
please...).

If you can describe *what* you are doing with your static variables,
it might help shed light on your memory issue... personally I try to
avoid static state in services, except for configuration state loaded
(for example) once in a static ctor.

Marc
Jan 11 '08 #13
A call to my webservice creates an instance of my dll:

MyWebMethod()
{
MyDll x = new MyDll();

x.DoSomeStuff();
}

In the the dll, MyDll, I wrote a destructor to prove x is always
released. This writes out a line of trace when called. It's only written
out when I stop IIS from running.

Please note that I have turned off the recycling at IIS and I assume
this is causing the problem since turning it back on (say every minute)
I start seeing my destructor code being written out.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '08 #14
The static are used as a status.

For example.

Call to webservice sets static variable to Started state.
Next caller, this variable needs to be set to previous callers state.

The statics work as way for indicating the mode my service is (Not
Logged in, Started etc) so must be persisted.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '08 #15
If the static variable is just status, you should be fine (locking
aside...) - bigger problem is if you have a big collection in the
static fields - which will of course sit their consuming memory...
hence why I raised this in connection with your memory concerns.

Marc
Jan 11 '08 #16
Yup, my statics are protected with a lock, so I don't think there is any
threading issues here.
The main concern is why the memory is NOT being released for the dll.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '08 #17
Its the whole class instance that concerns me.
The static need to hang around, and I accept the memory will not be
released. But the class that I create using instance variables etc is
never cleaned up.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '08 #18
But the class that I create using instance variables etc is
never cleaned up.
Well, it should be. If it isn't, then you're code is almost certainly
placing those controls somewhere publicly visible. Either that, or you
are using session-state and haven't ended the session - in which case
yes: the instance of the service implementation (per client) will be
retained. That is the pain of session state (and one of the many
reasons that I never use stateful web-services...)

Marc
Jan 11 '08 #19
I am pretty sure I am not using the Session (for example I have no code
which says Session["blah"] = myObj;

I just have a simple webservice which creates an instance of a dll. This
dll is not released when I turn of IIS recycle.

As a workaround, I now have a static counter and when it reaches
MAX_CALLS, run the GC :|
It cleans the memory up for me now.
*** Sent via Developersdex http://www.developersdex.com ***
Jan 14 '08 #20

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

Similar topics

12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
2
by: xzzy | last post by:
Using ASP.net, C# and a static class, does the following from MSDN mean: 1. a static hashtable would only live during the session and end when the session ends 2. there would be a different...
18
by: Jack | last post by:
Thanks.
6
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
7
by: Morgan Cheng | last post by:
In the book *Programming C#* 4th editionby Jesse Liberty, it reads "Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
32
by: mdh | last post by:
Hi all, When I try and initialize this static array, as in: void itoa_recursively(int n, char *s){ static char *p = s; it fails, yet this:
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: 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...
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
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.