Setting Global Objects in Global.asax

Tweet

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:


class="MyApp.globalVariableStore" scope="Application" />

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!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • johnnycomelately

    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.

  • http://www.miseldine.com miseldine

    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

  • Anonymous

    “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.

  • Sunil

    Need a help
    How can I use
    variable in the any .aspx page

  • Sandeep Gamare

    Hi,

    I have included the tag in global.asax, but my code refuses to compile in Visual Studio. It points to the good old “are you missing a using directive or an assembly reference”.

    Thanks
    Sandeep Gamare
    sandeep.gamare@gmail.com

  • Vathana

    When I write tag in global.asax file, It gives an error “object” is not an key identifier.Could you please help me on this.I write coding in vb.net