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

Question about static classes in ASP.Net 2.0

JC
Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?

Nov 27 '06 #1
4 1476
Dear JC.

when you create A static class. Only One copy remain in the Application
so far this is clear.
But do they share same copy or do they have different copy of them? The
answer is Yes they share the same copy of a static class.

try the sample code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Application["yahoo"] == null)
{
Random r = new Random();
Application.Add("yahoo", r.NextDouble());
}
Response.Write(TestClass.GetString());
}

}

public static class TestClass
{
public static string yahoostring = "Munna";
static TestClass()
{
Random r = new Random();
HttpContext.Current.Application["yahoo"] = r.NextDouble();
}
public static string GetString()
{
string s = yahoostring +
HttpContext.Current.Application["yahoo"].ToString();
return s;
}
}

Thanks
Md. Masudur Rahman
www.kaz.com.bd
KAZ Software Ltd.
Software outsourcing made simple...
JC wrote:
Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?
Nov 27 '06 #2
Hi,

JC wrote:
Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?
Actually, additionally to Masudur's answer, the static members or
classes are shared not only throughout the Web application, but
throughout the process. Since all the web applications run inside one
single process, it means that static classes and members are shared
between different web applications running on the same server. This is
why you have to handle them with a lot of care.

If you want to save variables inside the web application, but not share
them with other web applications, you use the Application collection
(similar to the Session collection).

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 27 '06 #3
This is close but not correct. Static things are 1 per AppDomain (as
marshalling is too expensive across them to do it by default).
ASP.net starts each web application in its own app domain, therefore .net
web applications will not share static objects.

Ciaran O'Donnell

"Laurent Bugnion" wrote:
Hi,

JC wrote:
Suppose an ASP.Net project contains a public static class with public
methods and members that are used throughout the application. Of course
being static, there is only copy of the class within the application.

Now suppose two users access the Web site simultaneously. Does each
user see his/her own single copy of the static class, or do they
share the class, thus creating a problem that can only be solved
if one use blocks the other while using resources in the static
class?

Actually, additionally to Masudur's answer, the static members or
classes are shared not only throughout the Web application, but
throughout the process. Since all the web applications run inside one
single process, it means that static classes and members are shared
between different web applications running on the same server. This is
why you have to handle them with a lot of care.

If you want to save variables inside the web application, but not share
them with other web applications, you use the Application collection
(similar to the Session collection).

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 27 '06 #4
Hi,

Ciaran O''Donnell wrote:
This is close but not correct. Static things are 1 per AppDomain (as
marshalling is too expensive across them to do it by default).
ASP.net starts each web application in its own app domain, therefore .net
web applications will not share static objects.

Ciaran O'Donnell
Oops I stand corrected. What confused me is that when you run a Web
application, static variables remain allocated until you restart the
ASP.NET process. I misinterpreted the consequences. Sorry for that.

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 27 '06 #5

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

Similar topics

12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
4
by: Robert Millman | last post by:
I have a question about the ASP.NET architecture and how classes are instantiated. When a web page invokes a static class and the class uses some static storage within the class, does the static...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
25
by: gordon | last post by:
I aksed a few days ago about static methods and got some good answers that were really useful. Now I am not sure if about the use of static on members (variables) and classes. Can someone...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
13
by: John Kraft | last post by:
Friends, I'm working on some crud stuff, and I was looking for opinions on the subject. Below, I have pasted some VERY simple sample code. Class2 is a "traditional" crud type object. In a...
9
by: Mike Hofer | last post by:
In a large application I'm working on (ASP.NET 1.1, VS2003), we have a base class that wraps stored procedures. Essentially, this base class (StoredProcedureBase) encapsulates the code to set up...
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.