Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to know if you could confirm certain static variables pattern on IIS 6 server.
Case 1 is quite clear.
But what about case 2?
Is my observation correct?

Case 1
When declared as part of a script of ASPX page e.g.:
<script runat="server">

static string verfilter = " ";
static string APIfilter = " ";

…some other code

The page is considered as a multi access method.
When a few users access the page simultaneously each user receives the value of static variables established a moment ago by any user of this page (last change state)

Case 2
When declared in code behind (in a separate *.cs file that belongs to a specific ASPX file), e.g.:

public partial class TestClass : System.Web.UI.Page
{
static int intEditor;
static string strName;

…some other code
}

It looks like the code behind of the page creates a separate object instance of the TestClass for each web user that opens the page.
It looks like that when a few users access the page simultaneously each user receives his own set of static variables.
Changes done by one web user do not affect other web users.
Posted
Updated 6-Jan-15 4:39am
v4

1 solution

A static field will always refer to the same memory location (in the same AppDomain), regardless of which instance of the class uses it. This will mean that all instances of TestClass have the same intEditor and strName. It doesn't matter what file the code sits in, at the end of the compilation process it's all the same MSIL.

If you need a per-instance variable, use standard properties.

Have a look at:
http://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx[^]
 
Share this answer
 
Comments
Moshe Ohrinovitch 6-Jan-15 8:46am    
In theory it sounds like a correct answer.
But in practice I could not confirm it.
I tested case 1 and case 2 and the results were surprising for me.
I anticipated to receive similar results in case 2 like in case 1 but the results were different.
Maybe Microsoft changed something recently with regards to scenarios like described in case 2?
Nathan Minier 6-Jan-15 9:08am    
I just wrote a quick test case and it worked as expected.
Program.cs
using System;

namespace StaticTest
{
class Program
{
static void Main(string[] args)
{
var one = new StaticTest("One","TestOne","ExternOne");

Console.WriteLine(one.Output);

var two = new StaticTest("Two", "TestTwo","ExternTwo");

Console.WriteLine(two.Output);
Console.WriteLine(one.Output);

var k = Console.ReadKey();

}
}

public partial class StaticTest
{
public static string staticItem { get; set; }
public string Name { get; set; }

public string Output { get { return string.Format("{0}: {1}", Name, staticItem); } }

public StaticTest(string name, string stat)
{
Name = name;
staticItem = stat;
}

}
}

testclass.cs
public partial class StaticTest
{
public static string externalTest { get; set; }

public string Output2 { get { return string.Format("{0}: {1} - {2}", Name, staticItem, externalTest); } }

public StaticTest(string name, string test, string externaltest) : this(name,test)
{
externalTest = externaltest;

}
}
Moshe Ohrinovitch 6-Jan-15 9:42am    
Your example is perfect.
But it is a console application.
My two examples are running on IIS server 6.1.
Nathan Minier 6-Jan-15 9:59am    
The functionality will be the same within an AppDomain in IIS, as the AppDomain is responsible for tracking variables and maintaining the application state.

The behavior you're describing sounds more like a possible issue with the output cache. Have you made sure that you are not sending cache'd responses?

http://www.iis.net/configreference/system.webserver/caching

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900