[C #. Net] ASP. NET Status Management 3: Application

Source: Internet
Author: User
Tags server memory
I,Global Application

From the word "application", we can see that the application state is global for the entire application. In the ASP era, we usually store some public data in the application, while ASP. the basic significance of the application in. Net has not changed: the number of data stored in the server memory is small and independent of the data requested by the user. Because the access speed is very fast and the data persists as long as the application does not stop, we usually initialize some data during application_start, which can be quickly accessed and retrieved in future access.

Let's try it out. First, right-click the website, select the "Add new project" command, as shown in 3-1, and select the global application class.

 

Figure 3-1 Add a global. asax

Global. asax (usually not renamed) is an event used to process the global application. Open the file and the system has defined some event handling methods for us.

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)

{

// Code that runs when the application starts

}

Void application_end (Object sender, eventargs E)

{

// Code that runs when the application is closed

}

Void application_error (Object sender, eventargs E)

{

// Code that runs when an unhandled error occurs

}

Void session_start (Object sender, eventargs E)

{

// The code that runs when the new session starts

}

Void session_end (Object sender, eventargs E)

{

// Code that runs at the end of the session

// Note: The session _

End event

// This event is not triggered if the session mode is set to StateServer or sqlserver

}

</SCRIPT>

Through these annotations, we can see that these events are the events of the entire application and have nothing to do with a page.

II,Use ApplicationWebsite access statistics

Suppose we want to use application to count Website access.

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

· Number of user shards. A user + 1 is introduced. Opening multiple pages by a user does 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 sessions is determined based on the Session. Therefore, you can add 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 the usage of the application is similar to that of the session. The only thing to note is that the application applies to the entire application. Many users may access the application at the same time, causing concurrency confusion. Therefore, you must first lock the application when modifying the application, after modification, unlock the device.

The number of page clicks is modified when page_load is displayed.

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 partitions: {0} <br/>", application ["uservisit"]);

}

}

Since we have initialized two variables at the beginning of the application, we can use them directly here. The first execution result is 3-2.

Refresh the page several times consecutively, as shown in Figure 3-3.

Press Ctrl + n to open several pages. You can see that the number of user shards remains unchanged. As described in the previous section, session is a copy of each client, not a copy of each browser.

Close the page and open it again. Since the session of the previous user has not timed out, the number of user sessions has increased by 1, 3-4.


Figure 3-2 use application
Website Statistics


Figure 3-3 page Click count
Page refresh Growth

Figure 3-4 caused by session_start
User growth

Visual Studio 2005 has a built-in server (not dependent on IIS ). Therefore, we cannot restart the application through IIS. 3-5.

Click the "stop" option and re-open the page, as shown in 3-6, we can see that both variables are reinitialized.

Figure 3-5 stopping the IDE's built-in web server Figure 3-6 restarting the Web server causes application_start to trigger

III, A PplicationSummary

In ASP. NET 2.0, application has become not very important. Because the application's self-management function is very weak, it does not have a session-like timeout mechanism. That is to say, the data in the application can only be deleted or modified manually to release the memory. As long as the application does not stop, the content in the application will not disappear. In the next section, we will see that the cache can be used to implement functions similar to the application, and the cache has a rich and powerful self-management mechanism.

Before the end of this section, let's summarize the features of the application based on the issues raised in section 1.

· Physical storage location. Server Memory.

· Storage type restrictions. Any type.

· Range of status usage. The entire application.

· Storage size limit. Any size.

· Lifecycle. Create an application at the beginning (precisely the first time the user requests a URL) and destroy the application at the end.

· Security and performance. Data is always stored on the server, which is highly secure but not easy to store too much data.

· Advantages and disadvantages and precautions. Data retrieval is fast, but data is not automatically released due to lack of self-management mechanism.

Related Article

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.