Introduction to global. asax

Source: Internet
Author: User

The global class derived from httpapplication is useful for managing Application Status and Request status.

The global. asax File Created by Visual Studio. Net generates a global class from the httpapplication class for each web application. This class contains many event handlers, such as application_start and session_start.

 

Some people may think that each web application has only one global class instance. In fact, in most application frameworks, the objects that represent the "application" are Singleton-that is, only one instance exists. We also know that the application_start event processing method is called only when the application starts to run ASP. NET. All these seem to imply that there is only one global object instance in our ASP. NET web application, but all these are indeed misleading!

 

ASP. NET Runtime (ASP. NET runtime, hereinafter referred to as "RunTime") maintains an httpapplication Object pool. When a request comes in, an httpapplication object is retrieved from the pool at runtime to serve the current request. The httpapplication object is always associated with the request and is only associated with the request, the request is processed. After the request is complete, the object will be returned to the pool during running. Later, the object will be retrieved from the pool to serve another request, but one request can only be associated with one httpapplication object at a time.

 

Application State Vs request State

Application Object) it is the place where global information is saved in the Web application. The application object is a convenient place for saving global information, for example, saving the connection string of the database:

Private void page_load (Object sender, system. eventargs E)

{

String connectionstring = application ["connectionstring"]. tostring ();

...

}

 

You can also declare static member variables in the httpapplication class to save application state information. For example, the database connection string in the previous example can be saved as follows.

Public class Global: system. Web. httpapplication

{

Public static readonly string connectionstring = "connection information ";

...

}

This static member variable can be accessed anywhere in ASP. NET Code, for example, string connectionstring = Global. connectionstring;

It is very important that if you want the string to be accessible globally, the string must be declared as a static member variable (you can also create a static attribute ).

On the contrary, if you use a general member variable (non-static) in the global type, you can only save the Request status. For example, the following code outputs the processing time (in milliseconds) of all requests in the debugging window)

Public class Global: system. Web. httpapplication

{

Protected datetime beginrequesttime;

Protected void application_beginrequest (Object sender, eventargs E)

{

Beginrequesttime = datetime. now;

}

Protected void application_endrequest (Object sender, eventargs E)

{

String messageformat = "elapsed request time (MS) = {0 }";

Timespan difftime = datetime. Now-beginrequesttime;

Trace. writeline (

String. Format (messageformat, difftime. totalmilliseconds ));

}

...

}

Now let's go back to the topic about saving the application status. Which method is better: Save the object reference in the Application object, or declare static members or attributes in the global class? Each method has its own advantages and disadvantages. Saving Global static members in the global class can make your data access have a strong type. Unlike the application object, you do not need to perform type conversion. The following code illustrates their differences:
Dataset cacheddata = (Dataset) application ["mydataset"];

String mystring = application ["mystring"]. tostring ();

 

Dataset cacheddata = Global. mydataset;

String mystring = Global. mystring;

 

Strong type makes your code clearer and stronger. This method can avoid performance loss caused by type conversion during runtime when application performance requirements are high. If you store value-type data, the strong type can also avoid performance loss caused by boxing and unboxing. In addition, the application object also has performance problems caused by lock caused by thread synchronization. If your global data is initialized only once and will not change any more, using static members in the global class can avoid performance loss caused by locking. However, if you use this method, we strongly recommend that you use an accessor (attribute) to ensure that the variable is read-only. If you want to read and write static member variables in the global class, remember to ensure thread security. The Application Object provides thread security guarantee by obtaining the read/write lock.

 

The safer place to initialize Global static member variables is in the application_start event handler. Even if there are many global object instances in the global scope, the application_start event handler is called only when the global object is created for the first time.

 

Application_beginrequest is ideal for initializing request state variables. In general, the request state variables do not require thread security because each global object only serves one request at a time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.