Lifetime of static member variable in ASP.NET

V

Vagif Abilov

I investigate further the problem I reported earlier as incorrectly set
values of static variable instances. Actually it is reproduced with a little
piece of code.

I made a simlpe Web site consisting of an empty page. The page C# code file
looks like this:

using System;
using System.Web;

namespace StaticMemberTest
{
public class Test
{
static int A
{
get { _Default.a++; return _Default.a; }
}
}

public partial class _Default : System.Web.UI.Page
{
internal static int a = 0;

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("A = " + a.ToString());
}
}
}

Steps to reproduce the problem:

1. Set the breakpoint within Page_Load
2. Start the debugger.
3. Once the breakpoint is reached, go to Watch window and type "Test.A".
4. This will trigger evaluation of Test.A getter which will increment the
value of _Default.a static variable.
5. Continue the execution, and you will see "A = 1" on the page.
6. Close the browser. Start the page now not in debug mode. It will still
display "A = 1".
7. Try to restart IIS and run the page without debugger. Still "A = 1".
8. If you try to run it in a debugger and evaluate Test.A, it will display
"A = 2". Etc.

The only way to reset the value of a static variable _Default.a is to change
the page source code, so Visual Studio will detect the change and rebuild
the page. Then _Default.a will be reset to 0.

I understand that assembly is compiled once and cached. But it looks that
also the state of static variables is stored. And this looks dangerous, and
this is not how it worked in .NET 1.1. Is this behaviour by design?


Vagif Abilov
Oslo Norway
 
?

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

Please don't cross post.

For an answer to this post, look in the thread in
microsoft.public.dotnet.framework.
 
V

Vagif Abilov

Thanks again, Goran.

Actually that was not cross-post. I made a post here and just pasted a copy
of it in another thread as answer to you.

Vagif
 
L

Laurent Bugnion

Hi,

Vagif said:
I investigate further the problem I reported earlier as incorrectly set
values of static variable instances. Actually it is reproduced with a little
piece of code.

I made a simlpe Web site consisting of an empty page. The page C# code file
looks like this:

using System;
using System.Web;

namespace StaticMemberTest
{
public class Test
{
static int A
{
get { _Default.a++; return _Default.a; }
}
}

public partial class _Default : System.Web.UI.Page
{
internal static int a = 0;

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("A = " + a.ToString());
}
}
}

Steps to reproduce the problem:

1. Set the breakpoint within Page_Load
2. Start the debugger.
3. Once the breakpoint is reached, go to Watch window and type "Test.A".
4. This will trigger evaluation of Test.A getter which will increment the
value of _Default.a static variable.
5. Continue the execution, and you will see "A = 1" on the page.
6. Close the browser. Start the page now not in debug mode. It will still
display "A = 1".
7. Try to restart IIS and run the page without debugger. Still "A = 1".
8. If you try to run it in a debugger and evaluate Test.A, it will display
"A = 2". Etc.

The only way to reset the value of a static variable _Default.a is to change
the page source code, so Visual Studio will detect the change and rebuild
the page. Then _Default.a will be reset to 0.

I understand that assembly is compiled once and cached. But it looks that
also the state of static variables is stored. And this looks dangerous, and
this is not how it worked in .NET 1.1. Is this behaviour by design?


Vagif Abilov
Oslo Norway

Static variables live as long as the process, in this case as long as
the IIS working process is not restarted. This was also the case in .NET
1.1. It's not linked with the DLL per se.

HTH,
Laurent
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top