473,834 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help explain what an "applicatio n instance" really is in terms of static data

I have a global.asax file with Application_Sta rt defined and create some
static data there and in another module used in the asp.net application and
I realize that static data is shared amongst child apps of an IIS
application and can be used by multiple users during the application life
cycle and for multiple page loads for the same or different page under a
root application.

What I don't understand and need to know is whether that static data would
be shared by ALL users of my application on a single server if my
application was loaded at all or whether it is possible to have my
application loaded multiple times such that each loading of my application
has its own set of static data that is potentially shared by multiple users.

Exactly what is an asp.net "applicatio n instance" in terms of windows
process, thread and object terminology which I understand well - is it a
user's sequenced use of the loaded assembly, concurrent thread usage of an
assembly or is it multiple loads of an assembly each of which can be used by
multiple users/threads?

As an analogy, let's say I write a multi threaded desktop app and allowed
multiple threads to use an object called X that was created by the main
thread. If that object X had static data then all threads in one process
that loaded my application would share that data in X even if each thread
had its own instance of X. But if I loaded 2 copies of my desktop
application (like running 2 copies of notepad.exe), each process would have
only one instance of object X static data no matter how many isntances of X
and each set of X static data would be shared by multiple threads in that
process but the staic data would not be shared between processes.

My concern is what scope I must lock at for all users of my asp.net
application on one machine. Can there be multiple copies of my static data?
One for each asp.net "applicatio n instance" or whether instance is just
referring to the fact that multiple users can be sharing the same static
data in the module that was loaded for the benefit of multiple users which I
believe I read are sequenced thru one at a time. Or do I really have to
worry about some users sharing one set of static data for one loading of my
application and another set of users sharing another set of static data
because my asp.net application was loaded again for them?

Hope I made myself clear. If I can have multiple sets of users each with
their own shared static data then I need to somehow lock across the multiple
copies of the static data.

So to summarize:

1. Can/must a C# lock be used for static data for ALL users on one machine -
depends if users are sequenced thru the application. Or is this totally
insufficient to protect a shared resource under asp.net?
2. Or rather must I somehow externally [like named semaphores] synchronize
across multiple application instances to protect that truly only one user at
a time on a given machine can modify a resource similar to what I would need
to do if I had multiple desktop processes running on one machine that had to
sequence use of a resource one at a time?

Whew...thanks!

Dave

Nov 16 '07 #1
4 4532
"static" means there is only one copy of the data. This is available to all
users of an application. If one user changes it, it is changed for all users.
You need to study the difference between the ASP.NET Application object vs.
what is "static" - they have similar features but are not the same thing. An
Application is a running instance of your web site in IIS, to put it in
simplest terms.

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com

"Dave" wrote:
I have a global.asax file with Application_Sta rt defined and create some
static data there and in another module used in the asp.net application and
I realize that static data is shared amongst child apps of an IIS
application and can be used by multiple users during the application life
cycle and for multiple page loads for the same or different page under a
root application.

What I don't understand and need to know is whether that static data would
be shared by ALL users of my application on a single server if my
application was loaded at all or whether it is possible to have my
application loaded multiple times such that each loading of my application
has its own set of static data that is potentially shared by multiple users.

Exactly what is an asp.net "applicatio n instance" in terms of windows
process, thread and object terminology which I understand well - is it a
user's sequenced use of the loaded assembly, concurrent thread usage of an
assembly or is it multiple loads of an assembly each of which can be used by
multiple users/threads?

As an analogy, let's say I write a multi threaded desktop app and allowed
multiple threads to use an object called X that was created by the main
thread. If that object X had static data then all threads in one process
that loaded my application would share that data in X even if each thread
had its own instance of X. But if I loaded 2 copies of my desktop
application (like running 2 copies of notepad.exe), each process would have
only one instance of object X static data no matter how many isntances of X
and each set of X static data would be shared by multiple threads in that
process but the staic data would not be shared between processes.

My concern is what scope I must lock at for all users of my asp.net
application on one machine. Can there be multiple copies of my static data?
One for each asp.net "applicatio n instance" or whether instance is just
referring to the fact that multiple users can be sharing the same static
data in the module that was loaded for the benefit of multiple users which I
believe I read are sequenced thru one at a time. Or do I really have to
worry about some users sharing one set of static data for one loading of my
application and another set of users sharing another set of static data
because my asp.net application was loaded again for them?

Hope I made myself clear. If I can have multiple sets of users each with
their own shared static data then I need to somehow lock across the multiple
copies of the static data.

So to summarize:

1. Can/must a C# lock be used for static data for ALL users on one machine -
depends if users are sequenced thru the application. Or is this totally
insufficient to protect a shared resource under asp.net?
2. Or rather must I somehow externally [like named semaphores] synchronize
across multiple application instances to protect that truly only one user at
a time on a given machine can modify a resource similar to what I would need
to do if I had multiple desktop processes running on one machine that had to
sequence use of a resource one at a time?

Whew...thanks!

Dave

Nov 16 '07 #2
static data is visible to all threads in the same ApplicationDoma in. the
appdomain represents an instance of the clr vm. each app domain has its
own memory, stack, code and heap (gc).

an actually program can host more have more than one appdomain, and
appdomain can talk to each other, but only thru remoting (even if in the
same nt process).

with asp.net there is a worker process per application pool. you can
config asp.net to use one pool (process) or more.

when an asp.net application is defined in IIS (its bound to a vdir), you
assign it to the pool. when the asp.net application (i'll call website
because there are too many application references) is started, an
appdomain is created and its loaded into it. normally there is only one
appdomain per website. so statics are shared between all users (threads)
of that website.

but when a website recycle happens (code changed, too much memory,etc),
a new appdomain is started, and the old one is shut down. if there any
request using the old appdomain, they complete while new requests use
the new appdomain. they do not see each others statics.

this last issue becomes important if you are referencing a unmanaged
dll. the unmanaged dll is actually loaded into the worker process, so
any statics in it are shared across both appdomains and in fact all
other websites using the same pool.

just to confuse things a little, there is a Application object, which
global.asx represents. the instances of these are maintained in a pool,
as each request gets its on unique instance. (this is for performace
reasons beyond the scope of this over simplified explanation). this is
why there is are begin and end events, hooking to create/dispose would
happen too often.

your application statics may or may not need locking. application begin
fires and completes before any other request has access to the
application. so read only can safely be loaded during this event without
locks. if it a read/write resource and does not sync access on its own,
then you need to use locks. c# has a lock statement you can use.
this is different than session, which has serialized access, as only one
request is processed at a time that uses the same session. this allows
concurrent request, just not to the same session data. this would be too
limiting for application access, so you need locks.

note: the application cache has serialized access to the object, but
does not sync methods/property accesses. the object should be thread
safe, or again you need to use locks.
-- bruce (sqlwork.com)
Dave wrote:
I have a global.asax file with Application_Sta rt defined and create some
static data there and in another module used in the asp.net application
and I realize that static data is shared amongst child apps of an IIS
application and can be used by multiple users during the application
life cycle and for multiple page loads for the same or different page
under a root application.

What I don't understand and need to know is whether that static data
would be shared by ALL users of my application on a single server if my
application was loaded at all or whether it is possible to have my
application loaded multiple times such that each loading of my
application has its own set of static data that is potentially shared by
multiple users.

Exactly what is an asp.net "applicatio n instance" in terms of windows
process, thread and object terminology which I understand well - is it a
user's sequenced use of the loaded assembly, concurrent thread usage of
an assembly or is it multiple loads of an assembly each of which can be
used by multiple users/threads?

As an analogy, let's say I write a multi threaded desktop app and
allowed multiple threads to use an object called X that was created by
the main thread. If that object X had static data then all threads in
one process that loaded my application would share that data in X even
if each thread had its own instance of X. But if I loaded 2 copies of my
desktop application (like running 2 copies of notepad.exe), each process
would have only one instance of object X static data no matter how many
isntances of X and each set of X static data would be shared by multiple
threads in that process but the staic data would not be shared between
processes.

My concern is what scope I must lock at for all users of my asp.net
application on one machine. Can there be multiple copies of my static
data? One for each asp.net "applicatio n instance" or whether instance is
just referring to the fact that multiple users can be sharing the same
static data in the module that was loaded for the benefit of multiple
users which I believe I read are sequenced thru one at a time. Or do I
really have to worry about some users sharing one set of static data for
one loading of my application and another set of users sharing another
set of static data because my asp.net application was loaded again for
them?

Hope I made myself clear. If I can have multiple sets of users each with
their own shared static data then I need to somehow lock across the
multiple copies of the static data.

So to summarize:

1. Can/must a C# lock be used for static data for ALL users on one
machine - depends if users are sequenced thru the application. Or is
this totally insufficient to protect a shared resource under asp.net?
2. Or rather must I somehow externally [like named semaphores]
synchronize across multiple application instances to protect that truly
only one user at a time on a given machine can modify a resource similar
to what I would need to do if I had multiple desktop processes running
on one machine that had to sequence use of a resource one at a time?

Whew...thanks!

Dave
Nov 16 '07 #3
Thanks so much Bruce. This clears up several issues understanding more what
happens. I forgot since this is managed code that the framework can do
things within a windows process that unmanaged code cannot such as
isolation. If you could bear with me I need clarification on a few points:

1. Sounds like in the normal case, ALL users of a website application shares
static data since there the code is loaded into only one appdomain. But
there could be 2 or more appdomains running the code in the case of recycle,
configuring multiple pools or I'm guessing in the case of a web garden in
which case there would be multiple instances of my static data loaded with
some users assigned to one and others to another. Did I understand that
right?

2. I understand the unmanaged dll situation and is not a concern for me.

3. I'm confused on your description of the Application object as to each
user getting their own instance. I thought that object was shared by all
users of the application even if there could possibly be multiple appdomains
running that application as in the cases you mentioned. 'Course they do say
"Applicatio n Instance" which I think is at the root of my confusion. What is
meant by "instance" in this case?

4. If Application_Sta rt runs when the application is loaded for the first
user, does it not have to finish before any other user enters the code? If
so then why would it matter if any sort of locking ws used or not for
readonly or readwrite resoruces? I probably misunderstood. Am unclear if
asp.net code has to be rentrant as I read once that users are serialized
thru an application but have my doubts as that would not scale very well.

5. Am also thinking that using "lock (static myobject)" would only work
within a single appdomain and that if in fact multiple appdomains had my
code loaded it would not serialize access amongst appdomains but only for
threads within an appdomain if for no other reason than that they had their
own copy of "myobject". Did you mean to say that somehow the C# lock would
work between appdomains or did I misunderstand?

6. Given, let's say, the possibility of multiple appdomains running the same
code wanting to update a file shared by all the child applications in an IIS
application, what would you recommend for synchronization so they don't step
on each other? A named semaphore or named mutex?

Thanks again,
Dave

"bruce barker" <no****@nospam. comwrote in message
news:OB******** ******@TK2MSFTN GP02.phx.gbl...
static data is visible to all threads in the same ApplicationDoma in. the
appdomain represents an instance of the clr vm. each app domain has its
own memory, stack, code and heap (gc).

an actually program can host more have more than one appdomain, and
appdomain can talk to each other, but only thru remoting (even if in the
same nt process).

with asp.net there is a worker process per application pool. you can
config asp.net to use one pool (process) or more.

when an asp.net application is defined in IIS (its bound to a vdir), you
assign it to the pool. when the asp.net application (i'll call website
because there are too many application references) is started, an
appdomain is created and its loaded into it. normally there is only one
appdomain per website. so statics are shared between all users (threads)
of that website.

but when a website recycle happens (code changed, too much memory,etc), a
new appdomain is started, and the old one is shut down. if there any
request using the old appdomain, they complete while new requests use the
new appdomain. they do not see each others statics.

this last issue becomes important if you are referencing a unmanaged dll.
the unmanaged dll is actually loaded into the worker process, so any
statics in it are shared across both appdomains and in fact all other
websites using the same pool.

just to confuse things a little, there is a Application object, which
global.asx represents. the instances of these are maintained in a pool, as
each request gets its on unique instance. (this is for performace reasons
beyond the scope of this over simplified explanation). this is why there
is are begin and end events, hooking to create/dispose would happen too
often.

your application statics may or may not need locking. application begin
fires and completes before any other request has access to the
application. so read only can safely be loaded during this event without
locks. if it a read/write resource and does not sync access on its own,
then you need to use locks. c# has a lock statement you can use.
this is different than session, which has serialized access, as only one
request is processed at a time that uses the same session. this allows
concurrent request, just not to the same session data. this would be too
limiting for application access, so you need locks.

note: the application cache has serialized access to the object, but does
not sync methods/property accesses. the object should be thread safe, or
again you need to use locks.
-- bruce (sqlwork.com)
Dave wrote:
>I have a global.asax file with Application_Sta rt defined and create some
static data there and in another module used in the asp.net application
and I realize that static data is shared amongst child apps of an IIS
application and can be used by multiple users during the application life
cycle and for multiple page loads for the same or different page under a
root application.

What I don't understand and need to know is whether that static data
would be shared by ALL users of my application on a single server if my
application was loaded at all or whether it is possible to have my
application loaded multiple times such that each loading of my
application has its own set of static data that is potentially shared by
multiple users.

Exactly what is an asp.net "applicatio n instance" in terms of windows
process, thread and object terminology which I understand well - is it a
user's sequenced use of the loaded assembly, concurrent thread usage of
an assembly or is it multiple loads of an assembly each of which can be
used by multiple users/threads?

As an analogy, let's say I write a multi threaded desktop app and allowed
multiple threads to use an object called X that was created by the main
thread. If that object X had static data then all threads in one process
that loaded my application would share that data in X even if each thread
had its own instance of X. But if I loaded 2 copies of my desktop
application (like running 2 copies of notepad.exe), each process would
have only one instance of object X static data no matter how many
isntances of X and each set of X static data would be shared by multiple
threads in that process but the staic data would not be shared between
processes.

My concern is what scope I must lock at for all users of my asp.net
application on one machine. Can there be multiple copies of my static
data? One for each asp.net "applicatio n instance" or whether instance is
just referring to the fact that multiple users can be sharing the same
static data in the module that was loaded for the benefit of multiple
users which I believe I read are sequenced thru one at a time. Or do I
really have to worry about some users sharing one set of static data for
one loading of my application and another set of users sharing another
set of static data because my asp.net application was loaded again for
them?

Hope I made myself clear. If I can have multiple sets of users each with
their own shared static data then I need to somehow lock across the
multiple copies of the static data.

So to summarize:

1. Can/must a C# lock be used for static data for ALL users on one
machine - depends if users are sequenced thru the application. Or is this
totally insufficient to protect a shared resource under asp.net?
2. Or rather must I somehow externally [like named semaphores]
synchronize across multiple application instances to protect that truly
only one user at a time on a given machine can modify a resource similar
to what I would need to do if I had multiple desktop processes running on
one machine that had to sequence use of a resource one at a time?

Whew...thank s!

Dave
Nov 16 '07 #4
Bruce, would dearly love to hear your view on my response to this message.
This one was so helpful. THe doc is not very clear on these sort of issues
or on reentrancy either. Thanks, Dave

"bruce barker" <no****@nospam. comwrote in message
news:OB******** ******@TK2MSFTN GP02.phx.gbl...
static data is visible to all threads in the same ApplicationDoma in. the
appdomain represents an instance of the clr vm. each app domain has its
own memory, stack, code and heap (gc).

an actually program can host more have more than one appdomain, and
appdomain can talk to each other, but only thru remoting (even if in the
same nt process).

with asp.net there is a worker process per application pool. you can
config asp.net to use one pool (process) or more.

when an asp.net application is defined in IIS (its bound to a vdir), you
assign it to the pool. when the asp.net application (i'll call website
because there are too many application references) is started, an
appdomain is created and its loaded into it. normally there is only one
appdomain per website. so statics are shared between all users (threads)
of that website.

but when a website recycle happens (code changed, too much memory,etc), a
new appdomain is started, and the old one is shut down. if there any
request using the old appdomain, they complete while new requests use the
new appdomain. they do not see each others statics.

this last issue becomes important if you are referencing a unmanaged dll.
the unmanaged dll is actually loaded into the worker process, so any
statics in it are shared across both appdomains and in fact all other
websites using the same pool.

just to confuse things a little, there is a Application object, which
global.asx represents. the instances of these are maintained in a pool, as
each request gets its on unique instance. (this is for performace reasons
beyond the scope of this over simplified explanation). this is why there
is are begin and end events, hooking to create/dispose would happen too
often.

your application statics may or may not need locking. application begin
fires and completes before any other request has access to the
application. so read only can safely be loaded during this event without
locks. if it a read/write resource and does not sync access on its own,
then you need to use locks. c# has a lock statement you can use.
this is different than session, which has serialized access, as only one
request is processed at a time that uses the same session. this allows
concurrent request, just not to the same session data. this would be too
limiting for application access, so you need locks.

note: the application cache has serialized access to the object, but does
not sync methods/property accesses. the object should be thread safe, or
again you need to use locks.
-- bruce (sqlwork.com)
Dave wrote:
>I have a global.asax file with Application_Sta rt defined and create some
static data there and in another module used in the asp.net application
and I realize that static data is shared amongst child apps of an IIS
application and can be used by multiple users during the application life
cycle and for multiple page loads for the same or different page under a
root application.

What I don't understand and need to know is whether that static data
would be shared by ALL users of my application on a single server if my
application was loaded at all or whether it is possible to have my
application loaded multiple times such that each loading of my
application has its own set of static data that is potentially shared by
multiple users.

Exactly what is an asp.net "applicatio n instance" in terms of windows
process, thread and object terminology which I understand well - is it a
user's sequenced use of the loaded assembly, concurrent thread usage of
an assembly or is it multiple loads of an assembly each of which can be
used by multiple users/threads?

As an analogy, let's say I write a multi threaded desktop app and allowed
multiple threads to use an object called X that was created by the main
thread. If that object X had static data then all threads in one process
that loaded my application would share that data in X even if each thread
had its own instance of X. But if I loaded 2 copies of my desktop
application (like running 2 copies of notepad.exe), each process would
have only one instance of object X static data no matter how many
isntances of X and each set of X static data would be shared by multiple
threads in that process but the staic data would not be shared between
processes.

My concern is what scope I must lock at for all users of my asp.net
application on one machine. Can there be multiple copies of my static
data? One for each asp.net "applicatio n instance" or whether instance is
just referring to the fact that multiple users can be sharing the same
static data in the module that was loaded for the benefit of multiple
users which I believe I read are sequenced thru one at a time. Or do I
really have to worry about some users sharing one set of static data for
one loading of my application and another set of users sharing another
set of static data because my asp.net application was loaded again for
them?

Hope I made myself clear. If I can have multiple sets of users each with
their own shared static data then I need to somehow lock across the
multiple copies of the static data.

So to summarize:

1. Can/must a C# lock be used for static data for ALL users on one
machine - depends if users are sequenced thru the application. Or is this
totally insufficient to protect a shared resource under asp.net?
2. Or rather must I somehow externally [like named semaphores]
synchronize across multiple application instances to protect that truly
only one user at a time on a given machine can modify a resource similar
to what I would need to do if I had multiple desktop processes running on
one machine that had to sequence use of a resource one at a time?

Whew...thank s!

Dave
Nov 17 '07 #5

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

Similar topics

10
4875
by: gregory_may | last post by:
I have an application I created called "JpegViewer.exe". It simply loads a Jpeg file and displays in on the screen. It works great, in my lab. When I am using it at a customer site, things change. Occasionally, it blows up with an Application Exception. It seems to only die at the customer site
4
5525
by: Piotr Perak | last post by:
Can someone explain the "object instance" term that is used i many of C# books? I have C++ background. In C++ instance and object meant the same. I could say that I have object of some class or instance of some class. There was no object instance used. How can I understand object instance? (I don't mean framweork class named object) -- Piotr (Peri, 3P) Perak
5
4977
by: TJS | last post by:
trying to display pdf file in browser fails on this line: Response.ContentType = "application/pdf" getting an error about no declaration found for "response" what declaration is needed ???
13
5102
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
43
3187
by: Christoph Schneegans | last post by:
Hi! Okay, so positions on "text/html" XHTML are totally contradicting. Anyway! I hope there's more consensus about "application/xml" XHTML. I've recently learned that Opera 9.0b2 does not only evaluate HTTP header, BOM and XML declaration to determine the character encoding of an XHTML document sent as "application/xml", but also the "meta" element. For example, <http://schneegans.de/sv/test-cases/?case=meta-only-encoding> is rendered...
9
1397
by: DJ Tuneman | last post by:
I've noticed that if I enable the "Application Framework" in the project for a Windows app, there's a bug when the programs load and I didn't even notice it until lately. If you click the "Show Desktop" icon on the task bar in Windows XP just prior to running the application, the application loads, shows the splash screen and then minimizes itself leaving the task bar item flashing orange. If you use the "Show Desktop" to minimize all...
1
6194
by: rguarnieri | last post by:
Hi!, I'm using Visual Studio 6.0 and I'm trying to open an asp page like a .pdf file, I used Response.ContentType="application/pdf" in my asp page but it doesn't work This is an example: <%@ Language=VBScript %> <%Response.ContentType="application/pdf"%>
0
9799
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9646
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9332
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7760
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3981
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.