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

D

Dave

I have a global.asax file with Application_Start 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 "application 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 "application 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
 
G

Guest

"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 said:
I have a global.asax file with Application_Start 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 "application 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 "application 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
 
B

bruce barker

static data is visible to all threads in the same ApplicationDomain. 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)

I have a global.asax file with Application_Start 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 "application 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 "application 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
 
D

Dave

Peter,

Thanks but this does not answer my question [which I overly stated I think]
which you misunderstood and would like you to reread if you would. I know
what static is just fine, at least from the desktop or server exe
application perspective and it is not true that there can only be one
instance of static data on a machine. In a desktop or server exe
application, if two .exe modules are loaded into memory as 2 separate
processes they each have their own copy of the static data in my program and
in fact do NOT share that data.

What I don't know is how asp.net works in this regard. If 1000 users all hit
the same web page at roughly the same time, will there truly be only one
copy of the static data for all those users or is it possible that
IIS/asp.net could start multiple appdomains/processes whatever that would
cause more than one instance of my static data to exist, say 400 using one
copy and 600 using the other. Perhaps this is only an issue if the case of a
web garden which supposedly starts additional worker threads though I'm not
sure what their effect is on instances of my code or data. I am not
concerned about the case of a web farm where different machines are
involved. One use of knowing how this works for sure is to determine if I
can do a "lock (object)" so as to serialize access to a chunk of code that
let's say updates a file shared by all child apps of an IIS application. I
have also read that asp.net serializes users through an application instance
which does not make a lot of sense to me as that would certainly ruin
performance scaling so am not sure if the asp.net app is expected to be
reentrant or not.

What I am asking is if there somehow can be multiple instances of my static
data on the same machine due to IIS loading multiple copies to help scale
servicing a large number of users. I have read both ways (see below) - that
all users truly share the same application data and that only users running
the same application share static or application data but that there can be
multiple instances loaded with each being shared by multiple users.

And for all purposes that I can see having indeed read about them I see no
discernable difference between using application state and static variables
in an asp.net application and would like you to point out any difference in
result. In fact, I have also read it is no longer suggested one use
application state.

Thanks,
Dave

From
http://findarticles.com/p/articles/mi_zddvs/is_200510/ai_n15717172/pg_1 -
talks about sets of users sharing different instances but in a manner that
does not clear things up:

"However, what I just said isn't the full story. You don't get just one
HttpApplication. That's where it gets bizarre. The ASP.NET system actually
creates multiple instances of HttpApplication (or of your derived class in
Global.asax) in order to handle multiple requests. The whole idea is so that
IIS and the ASP.NET system can handle high-performance Web sites that get
heavy traffic.
Even though there are multiple HttpApplication instances, there's still no
guarantee which of these are shared among which users. Each user accessing
the site does not necessarily get his own instance of HttpApplication. In
general, you don't want to make any assumptions about the individual
instances of HttpApplication. And remember, even though there may be
multiple HttpApplication instances, only one instance of the application
itself is running on the server."

---------------------------------------------------------------------------------------------------------------------------------------------------
Peter Bromberg said:
"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 said:
I have a global.asax file with Application_Start 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 "application 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 "application 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
 
D

Dave

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
"Application Instance" which I think is at the root of my confusion. What is
meant by "instance" in this case?

4. If Application_Start 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 said:
static data is visible to all threads in the same ApplicationDomain. 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)

I have a global.asax file with Application_Start 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 "application 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 "application 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
 
D

Dave

Hi Peter,

Here is at least one other opinion on static vs. application object where
they argue that static is better since the data can be in more readily
available form. And the second link argues it should not even be used. Truly
would like to know what you see as the differences. Thanks, Dave

http://www.bloggingdeveloper.com/po...iables-instead-of-the-Application-Object.aspx

http://findarticles.com/p/articles/mi_zddvs/is_200510/ai_n15717172/pg_7


Dave said:
Peter,

Thanks but this does not answer my question [which I overly stated I
think] which you misunderstood and would like you to reread if you would.
I know what static is just fine, at least from the desktop or server exe
application perspective and it is not true that there can only be one
instance of static data on a machine. In a desktop or server exe
application, if two .exe modules are loaded into memory as 2 separate
processes they each have their own copy of the static data in my program
and in fact do NOT share that data.

What I don't know is how asp.net works in this regard. If 1000 users all
hit the same web page at roughly the same time, will there truly be only
one copy of the static data for all those users or is it possible that
IIS/asp.net could start multiple appdomains/processes whatever that would
cause more than one instance of my static data to exist, say 400 using one
copy and 600 using the other. Perhaps this is only an issue if the case of
a web garden which supposedly starts additional worker threads though I'm
not sure what their effect is on instances of my code or data. I am not
concerned about the case of a web farm where different machines are
involved. One use of knowing how this works for sure is to determine if I
can do a "lock (object)" so as to serialize access to a chunk of code that
let's say updates a file shared by all child apps of an IIS application. I
have also read that asp.net serializes users through an application
instance which does not make a lot of sense to me as that would certainly
ruin performance scaling so am not sure if the asp.net app is expected to
be reentrant or not.

What I am asking is if there somehow can be multiple instances of my
static data on the same machine due to IIS loading multiple copies to help
scale servicing a large number of users. I have read both ways (see
below) - that all users truly share the same application data and that
only users running the same application share static or application data
but that there can be multiple instances loaded with each being shared by
multiple users.

And for all purposes that I can see having indeed read about them I see no
discernable difference between using application state and static
variables in an asp.net application and would like you to point out any
difference in result. In fact, I have also read it is no longer suggested
one use application state.

Thanks,
Dave

From
http://findarticles.com/p/articles/mi_zddvs/is_200510/ai_n15717172/pg_1 -
talks about sets of users sharing different instances but in a manner that
does not clear things up:

"However, what I just said isn't the full story. You don't get just one
HttpApplication. That's where it gets bizarre. The ASP.NET system actually
creates multiple instances of HttpApplication (or of your derived class in
Global.asax) in order to handle multiple requests. The whole idea is so
that IIS and the ASP.NET system can handle high-performance Web sites that
get heavy traffic.
Even though there are multiple HttpApplication instances, there's still no
guarantee which of these are shared among which users. Each user accessing
the site does not necessarily get his own instance of HttpApplication. In
general, you don't want to make any assumptions about the individual
instances of HttpApplication. And remember, even though there may be
multiple HttpApplication instances, only one instance of the application
itself is running on the server."

---------------------------------------------------------------------------------------------------------------------------------------------------
Peter Bromberg said:
"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 said:
I have a global.asax file with Application_Start 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 "application 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 "application 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
 
D

Dave

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 said:
static data is visible to all threads in the same ApplicationDomain. 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)

I have a global.asax file with Application_Start 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 "application 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 "application 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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top