Setting Global Objects in Global.asax

Share this article

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!

Frequently Asked Questions (FAQs) about Setting Global Objects in Global.asax

What is the purpose of the Global.asax file in ASP.NET?

The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules. The Global.asax file resides in the root directory of an ASP.NET application and is automatically detected and compiled by ASP.NET. It provides a way to centralize application and session event handling.

How do I create a Global.asax file?

To create a Global.asax file, you can simply add a new item to your ASP.NET project in Visual Studio and select the Global Application Class template. This will create a new Global.asax file with the necessary directives and event handlers.

What are some common uses of the Global.asax file?

The Global.asax file is commonly used for application-level event handling such as Application_Start, Application_End, Application_Error, Session_Start, and Session_End. It can also be used to set global variables, initialize application-wide settings, and handle application-level errors.

Can I have more than one Global.asax file in my ASP.NET application?

No, you can only have one Global.asax file in your ASP.NET application. The Global.asax file is an application-level file and is meant to handle events that occur at the application level.

Why was the Global.asax file removed in ASP.NET Core?

In ASP.NET Core, the functionality of the Global.asax file has been replaced by the Startup.cs file. The Startup.cs file is where you define the request handling pipeline and where you can configure services that are used by the application.

How do I handle errors in the Global.asax file?

You can handle errors at the application level in the Global.asax file by using the Application_Error event. This event is raised whenever an unhandled exception occurs in the application.

Can I access the HttpContext in the Global.asax file?

Yes, you can access the HttpContext in the Global.asax file. The HttpContext is available through the Context property of the HttpApplication object, which is passed to the event handlers in the Global.asax file.

How do I set global objects in the Global.asax file?

You can set global objects in the Global.asax file by declaring them as static members of the Global.asax class. You can then initialize these objects in the Application_Start event handler.

Can I use the Global.asax file in a Web API project?

Yes, you can use the Global.asax file in a Web API project. However, in ASP.NET Core, the functionality of the Global.asax file has been replaced by the Startup.cs file.

How do I debug the Global.asax file?

You can debug the Global.asax file by setting breakpoints in the event handlers. When an event is raised, the execution will stop at the breakpoint, allowing you to inspect the state of the application.

Philip MiseldinePhilip Miseldine
View Author

Philip is a Computer Science PhD student at Liverpool John Moores University. He's still not mastered guitar tabs, never finished Mario, and needs a haircut.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week