Global.asax and HTTPApplication class

zhaozj2021-02-11  162

Global.asax and HTTPApplication class

There are many uses from HTTPApplication derived Global class, including managing applications and request status.

The Global.asax file created by Visual Studio.net generates a Global class that is scheduled from the HTTPApplication class, which contains many event handles, such as Application_Start and Session_Start.

Some people think that each web application has only one instance of a Global class. In fact, in most applications frameworks, representative "Application" objects are Singleton - that there is only one instance exists. We also know that ASP.NET is running only when the application starts at the beginning of the application. All these seem to hint, only one of the Global objects in our ASP.NET web application, but all of this is indeed misleading!

ASP.NET runtime (ASP.NET Runtime, hereinafter referred to as "runtime") maintains an HTTPApplication object pool. When a request comes in, take an HTTPApplication object from the pool to serve the current request, the HTTPApplication object is associated with the request, and only associated with the request until the request is processed. When the request is completed, the object is returned to the pool when the runtime will take the service from the pool to the other request - but a request can only be associated with an HTTPApplication object each time.

Application

State

VS

REQUEST

State

Application object (HTTPApplicationState type, the translator is pressing an attribute of the HTTPApplication class, which is the only web application. The web application creates the first time when there is a request to come in.) Is our WEB application to save the overall situation Where the information, Application object is a convenient place to save global information, such as a connection string that saves databases:

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 status information, for example, the database connection string in the above example can be saved as follows.

Public Class Global: System.Web.httpApplication

{

Public Static

Readonly string connectionstring = "connection information";

.

}

The static member variable can be accessed anywhere in the ASP.NET code, for example

String connectionstring = global.connectionstring;

Very important point is that if you want the string to be accessed in the global range, the string must declare a static member variable (you can also create a static property).

Conversely, if you use a general member variable (non-static) in the Global type, you can only save the request status, give an example to explain that the following code will output all request processing time (milliseconds) in the debug 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));

}

.

}

Ok, let us return to the topic about saving the application status. That way the method is better: Save the reference to the object in the Application object, or declare a static member or attribute in the Global class? Various ways have advantages and disadvantages.

Save Global Static members in the Global class allows your data access with strong types, not like Application objects, you don't need to conversion, the following code illustrates their difference:

DataSet CachedData = (Dataset) Application ["MyDataSet"];

String myString = Application ["mystring"]. TOSTRING ();

DataSet CachedData = Global.myDataSet;

String mystring = global.mystring;

Strong type makes your code more clear and strong, in case the application performance requirements, this approach can avoid performance losses from running type conversion. If you save the value type of data, strong types can avoid the performance loss caused by boxing and unloading. In addition, the Application object also has problems with the performance caused by the lock caused by thread synchronization. If your global data is only initialized once, you will never change, and the static members in the Global class can avoid locking the performance losses. However, if you use this way, you mean it to recommend that you use the accessor (attribute) to ensure that the variable is read-only. If you want to read the static member variables in the Global class, remember to ensure that threads are secure. Application objects provide thread security guarantees by getting read and write locks.

Compare Safe Initialization Global Static member variables are in the Application_Start event handler. Even if the global range has instances of the Global object, the Application_Start event handler will only be called when the Global object is created when the global object is created.

The more ideal initialization request status variable is Application_BeginRequest, requiring status variables to generally do not need thread security, because each Global object is only served at the same time

BY K. Scott Allen (Scott @ Odetocode.com)

转载请注明原文地址:https://www.9cbs.com/read-5700.html

New Post(0)