Detailed Explanation of Application in ASP. NET

  • 2021-07-18 07:46:44
  • OfStack

1. Global application classes

From the word Application, we can roughly see that Application state is global to the whole application. In the era of ASP, we usually store some public data in Application, but the basic meaning of Application in ASP. NET has not changed: store a small amount of data independent of user requests in server memory. Because its access speed is very fast and data 1 exists as long as the application does not stop, we usually initialize some data at Application_Start, which can be accessed and retrieved quickly in future visits.

Global. asax is an event that handles application globals. Open the file, and the system has defined the handling methods of 1 events for us.


void Application_Start(object sender, EventArgs e)
{
    // Code that runs when the application starts
}  
 
void Application_End(object sender, EventArgs e)
{
    //  Code to run when the application closes
}      
 
void Application_Error(object sender, EventArgs e)
{
    // Code to run when an unhandled error occurs
}
 
void Session_Start(object sender, EventArgs e)
{
    // Code to run when a new session starts
}
 
void Session_End(object sender, EventArgs e)
{
    // Code that runs at the end of the session
 
    // Attention : Only when Web.config In the file sessionstate Mode set to InProc Will be raised when Session_End Events
 
    // If the session mode is set to StateServer Or SQLServer The event is not raised
}

From these comments, we can see that these events are the events of the whole application and have nothing to do with a certain 1 page.

2. Application of Application Object


1. Save information using an Application object

(1) Use Application object to save information

Application ("key name") = value
Or Application ("key name", value)

(2) Obtaining Application object information

Variable name = Application ("key name")
Or: Variable name = Application. Item ("key name")
Or: Variable name = Application. Get ("key name")

(3) Update the value of the Application object

Application. Set ("Key name", value)

(4) Delete 1 key

Application. Remove ("Key name", value)

(5) Delete all keys

Application.RemoveAll()
Or Application. Clear ()

2. It is possible for multiple users to access the same Application object at the same time

In this way, it is possible for multiple users to modify the same Application named object, resulting in the problem of different data.
The HttpApplicationState class provides two methods, Lock and Unlock, to address access synchronization to Application objects, allowing only one thread at a time to access application state variables.

About Locking and Unlocking

Lock: Application. Lock ()
Access: Application ("key name") = value
Unlock: Application. Unlock ()
Note: Lock method and UnLock method should be used in pairs.
Can be used for website visitors, chat rooms and other equipment

3. Using Application Events

A special optional file can be included in the ASP. NET application-the Global. asax file, also known as the ASP. NET application file, which contains code for responding to application-level events raised by the ASP. NET or HTTP modules.

3. Use the Application statistics website to visit

Suppose we want to use Application to count website visits.

Number of page clicks. The page is clicked 1 time +1, regardless of whether the same user clicks the page multiple times.

Number of user visits. One user +1 comes, and opening multiple pages by one user will not affect this number.

We first need to initialize two variables in Application_Start.


void Application_Start(object sender, EventArgs e)
{
    // Code that runs when the application starts
    Application["PageClick"]=0;
    Application["UserVisit"]=0;
}

The number of user accesses is determined according to Session, so you can increase this variable at Session_Start:


void Session_Start(object sender, EventArgs e)
{
    Application.Lock();
    Application["UserVisit"]=(int)Application["UserVisit"]+1;
    Application.UnLock();
}

We can see that Application is used in a similar way to Session. The only thing to note is that the scope of Application is the whole application, and many users may access Application at the same time, causing concurrency confusion. Therefore, when modifying Application, it is necessary to lock Application first, and then unlock it after modification.

The number of page clicks is modified at the time of page Page_Load.


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Application.Lock();
        Application["PageClick"] = (int)Application["PageClick"] + 1;
        Application.UnLock();
        Response.Write(string.Format(" Number of page clicks: {0}<br/>", Application["PageClick"]));
        Response.Write(string.Format(" Number of user accesses: {0}<br/>", Application["UserVisit"]));
    }
}

4. Application Summary

In ASP. NET 2.0, Application has become less important. Because the Application is very weak in self-management, it does not have a timeout mechanism similar to the Session. In other words, the data in Application can only be deleted or modified manually to free memory, and the contents in Application will not disappear as long as the application does not stop. In the next section, we will see that Cache can be used to implement functions similar to Application, while Cache has rich and powerful self-management mechanism.

Let's summarize the features of Application under 1.

Physical location of storage. Server memory.

Type restrictions of storage. Any type.

The scope of state use. The entire application.

Size limit of storage. Any size.

Life cycle. The application is created at the beginning (to be precise, when the user requests an URL for the first time) and destroyed at the end of the application.

Safety and performance. Data is always stored on the server side, which has high security, but it is not easy to store too much data.

Advantages and disadvantages and precautions. The retrieval speed of data is fast, but there is no self-management mechanism, and the data will not be released automatically.


Related articles: