From a forum request :)
ASP.NET includes a Global.asax file which exposes many application-wide events which you can use to control your application execution.
It also allows you to define globally-scoped objects. However, a word of caution…as each page in an ASP.NET page runs in its own thread, you need to make sure your objects are thread safe for multiple users requesting your site.
Why is this important? Imagine 2 requests, from user A and user B. Each request runs in a separate thread, and user A manipulates a global object in one thread, and user B manipulates the same object in a different thread. Depending on Windows, these might be executed in any order…meaning your global object won’t be synchronized for both users.
So to avoid this complication, use thread-safe objects, or, of particular interest, use a hashtable as a thread-safe container for your variables:
using System;
using System.Collections;
namespace MyApp.Collections {
public class globalVariableStore{
private Hashtable myCollection;
private Hashtable threadSafeCollection;
public globalVariableStore()
{
myCollection = new Hashtable();
threadSafeCollection = Hashtable.Synchronized(myCollection);
}
public void addVariable(string name, object value)
{
threadSafeCollection[name] = value;
}
public object retrieveVariable(string name) {
return threadSafeCollection[name];
}
}
}
You can then define an instance of your globalVariableStore as globally-scoped by adding a line to your global.asax file:
You can now access the object through “myVariables” from anywhere in your application.
Alternatively, you can use the Application object to store variables, such as:
Application["myvariable"] = "foo";
Yet, creating your own global objects can give far greater depth and control over globally defined object instances…just make sure they are thread safe!
Persistent Storage
Yet another mechanism is to add string values to your web.config file, which are then always available to your application:
This should be placed under the “configuration” element in the file.
You can then retrieve the value thus:
string connectionString = ConfigurationSettings.AppSettings("connectionString");
Remember, web.config files can’t be served over your server to a browser, so this affords some security, but valuable information is stored in plain-text on your server, so it might be an idea to encrypt some sensitive strings beforehand!






If you are using VB.NET i believe you can use the Module , which is a kind of ’sealed’ class (non-inheritable), and can be used to store global variables. Also, since it is a ’shared’ class, it need not be instantiated.
February 28th, 2004 at 2:32 am
Yes. The VB.NET compiler essentially converts the “module” keyword into what in c# is known as an “internal sealed class” and therefore makes all public variable static. In addition, referneces to a public module variable gets “module” added to it as well.
The upshot being, since static variables are the same across all threads any time a variable is updated in one thread, all threads update :)
Nice comment :D
February 28th, 2004 at 12:56 pm
“so it might be an idea to encrypt some sensitive strings beforehand”
You say, do you have any good link to tutorials for this? Encrypting in .NET that is.
March 1st, 2004 at 6:13 am
Need a help
How can I use variable in the any .aspx page
September 10th, 2004 at 7:44 am
Hi,
I have included the
Thanks
Sandeep Gamare
sandeep.gamare@gmail.com
November 1st, 2004 at 5:56 pm
When I write
December 10th, 2004 at 12:37 am