473,696 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shared variables in ASP.NET environment

A consequence of the ASP.NET architecture on IIS has just hit home with a
big thud. It's to do with shared variables. Consider a module like this:

Public Module Functions

Public GlobalName As String ' this is ineffect a global
application object

End Module

As there is one aspnet_wp.exe process shared by all users, would all users
access the same GlobalName variable. So if user A runs the application and
sets it to "Fred" and user B sets it to "Bert", then User A sees the new
value?

This this right?

Cheers, Rob.
Nov 19 '05 #1
15 4935
> As there is one aspnet_wp.exe process shared by all users, would all users
access the same GlobalName variable. So if user A runs the application and
sets it to "Fred" and user B sets it to "Bert", then User A sees the new
value?


Answering my own question but yes, this is what happens. All users access
the same shared variables...

Cheers, Rob.
Nov 19 '05 #2
Hi,

Store it in session state.

Session("Global Name")="Rob Nicholson"

http://msdn.microsoft.com/library/de...ssionstate.asp

Ken
------------------
"Rob Nicholson" <in******@commu nity.nospam> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
A consequence of the ASP.NET architecture on IIS has just hit home with a
big thud. It's to do with shared variables. Consider a module like this:

Public Module Functions

Public GlobalName As String ' this is ineffect a global
application object

End Module

As there is one aspnet_wp.exe process shared by all users, would all users
access the same GlobalName variable. So if user A runs the application and
sets it to "Fred" and user B sets it to "Bert", then User A sees the new
value?

This this right?

Cheers, Rob.

Nov 19 '05 #3
> Session("Global Name")="Rob Nicholson"

Hi Ken - I thought about that but the problem was that the variable was
needed deep in a middle tier layer which, I didn't think, new about the
current session. However, I then found:

System.Web.Http Context.Current .Session("Globa lName")

which means the middle tier can get at the session cache without having to
pass a pointer to it all over the place.

I assume that System.Web.Http Context.Current is in effect a shared (static)
property that's available process wide and returns the HttpContext object
for the current request being processed which in turn gives the user's own
session state.

Cheers, Rob.
Nov 19 '05 #4
I clarified this for you a minute ago in another thread. However, I will
repeat my clarification for you:

"Global" and "Shared" are 2 different terms, with 2 different meanings. An
object with global scope (accessibility) may or may not be thread-safe. A
static (Shared) object is certainly NOT thread-safe. Now, this is not the
issue which you addressed specifically, but I am not sure what you meant by
the term "Shared." Any Shared (static) memeber of any class in an
application is, by definition, globally scoped to the application. You don't
even need to create an instance of it, or any class it may reside in. If the
class is included in your application, it is available to the entire
application, and it is globally scoped.

Putting an object into the Application Collection (which, BTW, is not
thread-safe in itself), or into the Application Cache (which is thread-safe
in itself) makes the object global to the application as well. However, in
this case, it is an instance of a class, not a singleton object residing in
the application heap. If you put an object into the Application Collection
or Application Cache, and the object itself is not thread-safe (such as a
Collection, for example), you will run into a whole different set of
problems, as multiple threads may try to access the object at the same time.
A Collection is the easiest example to illustrate. Imagine one thread
attempting to iterate through a Collection while another thread is removing
an item from the Collection.

Now, your remark is in regards to the fact that putting an object into the
Application Collection or the Application Cache makes it globally available,
and thus, when one thread changes its value, it is changed for all threads.
And that is indeed a problem, if you plan to change its value in, for
example, any page used by any user in your app. I just wanted you to
understand the terms, and the possible issues you may encounter.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Rob Nicholson" <in******@commu nity.nospam> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
A consequence of the ASP.NET architecture on IIS has just hit home with a
big thud. It's to do with shared variables. Consider a module like this:

Public Module Functions

Public GlobalName As String ' this is ineffect a global
application object

End Module

As there is one aspnet_wp.exe process shared by all users, would all users
access the same GlobalName variable. So if user A runs the application and
sets it to "Fred" and user B sets it to "Bert", then User A sees the new
value?

This this right?

Cheers, Rob.

Nov 19 '05 #5
CORRECTION!

Sorry to make my clarification unclear, but that last paragraph certainly
WAS unclear (and incorrect). I said that you were referring to using the
Application to store objects, when, in fact, you were referring to a Module,
which, by definition, contains all Shared (static) members. And, of course,
you wouldn't be storing Module members in the Application!

Apologies,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Rob Nicholson" <in******@commu nity.nospam> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
A consequence of the ASP.NET architecture on IIS has just hit home with a
big thud. It's to do with shared variables. Consider a module like this:

Public Module Functions

Public GlobalName As String ' this is ineffect a global
application object

End Module

As there is one aspnet_wp.exe process shared by all users, would all users
access the same GlobalName variable. So if user A runs the application and
sets it to "Fred" and user B sets it to "Bert", then User A sees the new
value?

This this right?

Cheers, Rob.

Nov 19 '05 #6
> the term "Shared." Any Shared (static) memeber of any class in an
application is, by definition, globally scoped to the application. You don't

Actually, this is in a module and I assume that modules are in effect
shared:

Public Module Functions

Public GlobalName As String

End Module

Functions.Globa lName appears to be accessible from everywhere.
understand the terms, and the possible issues you may encounter.


We were using something akin to above but actually storing a reference to
our own application object so actually we had:

Public Module Functions

Public GlobalApp As OurApp

End Module

This was a no-no as all users (sessions) started sharing the same OurApp
which could have started the thread-safe problems you describe, e.g. one
user could clear OurApp.BigColle ction whilst another user was using it.

I've made a simple change to our application environment to store OurApp in
the session cache instead which, AFAIK, will be fine as each other now gets
their own instance of OurApp.

Realising that all users are running in the same process with the same
instance of the DLLs et all plus shared memory and heap was a important
realisation for me. The penny dropped.

I've done a fair amount of multi-thread stuff in the past but not thought
about it too much in the ASP.NET environment.

Cheers, Rob.
Nov 19 '05 #7
Hi Rob,

Yes, see my self-correction which I posted almost immediately after the post
you reponded to. I realized that you were not talking about using the
Application to store data, but a Module. It is true that every member of a
Module is Shared (static), a singleton that is available throughout the
application.

Modules are a special kind of class, another accomodation of Microsoft for
VB6 developers. However, it is not good practice to use Modules at all, as
they present no accessibility control whatsoever. In addition, they often
confuse VB developers who are accustomed to using them in executable
applications, and do not understand the consequences of using them is a web
application, particularly one that is multi-threaded. Instead, use classes.
You can create classes which have both Shared (static) members and members
with other (instance-type) accessibility as well, which gives you full
control over the members in the class.

Shared (static) objects present a number of difficulties in a multi-threaded
environment. That is not to say that they are not useful. Shared methods,
for example, are often quite useful, as they reduce the stack size, and
therefore memory usage. Shared fields and properties are problematic, but
useful in certain situations (particularly when the classes which use them
do not change them). One excellent way of making sure that client classes do
not change them is to create a private Shared field, and expose it via a
public Shared read-only property. Example:

Public Class Foo

Private Shared _Bar As String
Public Shared ReadOnly Property Bar() As String
Get
Return _Bar
End Get
End Property

' Public Constructor
Public Shared Sub New()

End Sub

' Shared Constructor
Shared Sub New()
_Bar = "Foo Bar"
End Sub

End Class

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Rob Nicholson" <in******@commu nity.nospam> wrote in message
news:e2******** ******@TK2MSFTN GP12.phx.gbl...
the term "Shared." Any Shared (static) memeber of any class in an
application is, by definition, globally scoped to the application. You

don't

Actually, this is in a module and I assume that modules are in effect
shared:

Public Module Functions

Public GlobalName As String

End Module

Functions.Globa lName appears to be accessible from everywhere.
understand the terms, and the possible issues you may encounter.


We were using something akin to above but actually storing a reference to
our own application object so actually we had:

Public Module Functions

Public GlobalApp As OurApp

End Module

This was a no-no as all users (sessions) started sharing the same OurApp
which could have started the thread-safe problems you describe, e.g. one
user could clear OurApp.BigColle ction whilst another user was using it.

I've made a simple change to our application environment to store OurApp
in
the session cache instead which, AFAIK, will be fine as each other now
gets
their own instance of OurApp.

Realising that all users are running in the same process with the same
instance of the DLLs et all plus shared memory and heap was a important
realisation for me. The penny dropped.

I've done a fair amount of multi-thread stuff in the past but not thought
about it too much in the ASP.NET environment.

Cheers, Rob.

Nov 19 '05 #8
Kevin,

I can be mistaken however reading your messages, are you in my opinion
missing the point which is that a shared variable in an ASPNET persistent
however it is part of the application (as it is everywhere), however in
ASPNET belongs an application to all clients.

Persistent to a client action (postback) in ASPNET are
A viewstate item (belongs to the page)
A session item (belongs as IIS part to the client)
A shared variable (belongs to the application)
A cache item (is used by the total application)

A global item in ASNET is not persistent in the way as windowsforms where it
is almost forever persitent.

I hope this helps,

Cor

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> schreef in bericht
news:uo******** *****@TK2MSFTNG P12.phx.gbl...
Hi Rob,

Yes, see my self-correction which I posted almost immediately after the
post you reponded to. I realized that you were not talking about using the
Application to store data, but a Module. It is true that every member of a
Module is Shared (static), a singleton that is available throughout the
application.

Modules are a special kind of class, another accomodation of Microsoft for
VB6 developers. However, it is not good practice to use Modules at all, as
they present no accessibility control whatsoever. In addition, they often
confuse VB developers who are accustomed to using them in executable
applications, and do not understand the consequences of using them is a
web application, particularly one that is multi-threaded. Instead, use
classes. You can create classes which have both Shared (static) members
and members with other (instance-type) accessibility as well, which gives
you full control over the members in the class.

Shared (static) objects present a number of difficulties in a
multi-threaded environment. That is not to say that they are not useful.
Shared methods, for example, are often quite useful, as they reduce the
stack size, and therefore memory usage. Shared fields and properties are
problematic, but useful in certain situations (particularly when the
classes which use them do not change them). One excellent way of making
sure that client classes do not change them is to create a private Shared
field, and expose it via a public Shared read-only property. Example:

Public Class Foo

Private Shared _Bar As String
Public Shared ReadOnly Property Bar() As String
Get
Return _Bar
End Get
End Property

' Public Constructor
Public Shared Sub New()

End Sub

' Shared Constructor
Shared Sub New()
_Bar = "Foo Bar"
End Sub

End Class

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

"Rob Nicholson" <in******@commu nity.nospam> wrote in message
news:e2******** ******@TK2MSFTN GP12.phx.gbl...
the term "Shared." Any Shared (static) memeber of any class in an
application is, by definition, globally scoped to the application. You

don't

Actually, this is in a module and I assume that modules are in effect
shared:

Public Module Functions

Public GlobalName As String

End Module

Functions.Globa lName appears to be accessible from everywhere.
understand the terms, and the possible issues you may encounter.


We were using something akin to above but actually storing a reference to
our own application object so actually we had:

Public Module Functions

Public GlobalApp As OurApp

End Module

This was a no-no as all users (sessions) started sharing the same OurApp
which could have started the thread-safe problems you describe, e.g. one
user could clear OurApp.BigColle ction whilst another user was using it.

I've made a simple change to our application environment to store OurApp
in
the session cache instead which, AFAIK, will be fine as each other now
gets
their own instance of OurApp.

Realising that all users are running in the same process with the same
instance of the DLLs et all plus shared memory and heap was a important
realisation for me. The penny dropped.

I've done a fair amount of multi-thread stuff in the past but not thought
about it too much in the ASP.NET environment.

Cheers, Rob.


Nov 19 '05 #9
> VB6 developers. However, it is not good practice to use Modules at all, as
they present no accessibility control whatsoever. In addition, they often


We don't use them much at all - our Functions module has just three system
wide modules. And in fact, they could probably be included in the basic App
class anyway. I saw the OOPs light many years ago :-)

Cheers, Rob.
Nov 19 '05 #10

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

Similar topics

3
1807
by: Joe Fallon | last post by:
I have a Shared varibale in a base class and all the Shared methods in the sub-classes use it (and CHANGE it). I thought I was saving myself the "trouble" of Dimming this variable inside each Shared method. But now I wonder if I will have a problem in a multi-user environment with code that changes this variable. Can someone please review this sample code and let me know if it is an issue? Also what is the best way to clean it up? Dim...
7
9292
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them from my code. My stab at the problem is shown below. Can anyone suggest what my most efficient workable solution would be (i.e. I don't want to have to create instances of the classes as they will only store constant information and I would...
14
1756
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As String ' this is ineffect a global application object End Module
5
4390
by: Confused ! | last post by:
I am writing an ASP.NET application and I want to use shared members on the data layer to retreive information from the SQL Server. If I declare a Function to be Shared, how will this affect things in a multi user environment. In other words, are my shared classes for the want of a better word. "Instantiated" for each user. Hope this makes sense ! --
11
1265
by: Mr Newbie | last post by:
I'm still thinking around this subject, and I find myself unsure about a couple of things. When a new users launches an ASP.NET application, this must be on a new thread. When it encounters a shared member in a class, unless you are setting static variables presumably when you instantiate an local object in that shared member function the system maintains a local copy of that object per thread ( is this right ? ). To perhaps make my...
5
1576
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We have used shared properties on some of these objects to enable caching of frequently used objects, so save fetching them from SQL every time. These shared properties vastly increase performance of our windows app.
3
24330
by: gmax2006 | last post by:
Hi, I am using RedHat Linux 4. and I developed an oracle 10g based application by using cx_Oracle (cx_Oracle-4.1-10g-py23-1.i386.rpm) and Python 2.3.4. When I run the application through direct console connection, It works perfect. But, when I schedule a crontab job to run the application, It logs this
2
939
by: Michael.Thomlinson | last post by:
Hello fellow coders, I have run into an issue that is otherwise painfull for my brain to think about at this point so i turn to you guys for input. Scenario. For my application i have chosen to make my own "Environment" This environment contains Variables and Methods that are Shared.
0
974
by: netkadirisani | last post by:
Hi, i built a dll (used vb.net) that has some shared variables. the basic idea of using the shared variables is to read some values from the database and save them in the shared variables during the first access of dll and use them then on thus avoiding any more database hits. when i use the dll on my machine, it works perfect. i.e., it reads the values from database, saves them to shared variables on one time basis. but when i installed the...
0
8656
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
8590
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
9005
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8869
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8845
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7693
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...
0
5848
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3025
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
3
1988
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.