Application in asp.net-basic application

Source: Internet
Author: User
Tags server memory

One, global application class

From the Word application, you can see that the application state is the entire application global. In the ASP era we usually store some public data in the application, while the basic meaning of application in asp.net is unchanged: storing less data in server memory and independent of user requests. Because it is accessed very quickly and as long as the application does not stop and the data persists, we typically initialize some data at the time of Application_Start, which can be accessed and retrieved quickly in future visits.

Global.asax is an event that is used to handle the global application. To open the file, the system has defined some of the handling methods for the event.

Copy Code code as follows:

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 shuts down
}

void Application_Error (object sender, EventArgs e)
{
Code that runs when an unhandled error occurs
}

void Session_Start (object sender, EventArgs e)
{
Code that runs when a new session starts
}

void Session_End (object sender, EventArgs e)
{
Code that runs at the end of a session

Note: The Session_End event is raised only if the sessionstate mode in the Web.config file is set to InProc

If the session mode is set to StateServer or SQL Server, the event is not raised
}

As we can see from these annotations, these events are an event for the entire application and have nothing to do with a page.

II. Application of Application Object


1. Save information using the Application object

(1), use Application object to save information

Application ("key name") = value
or application ("Key Name", value)

(2), get 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 a 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

This makes it possible for multiple users to modify the same application named object, resulting in inconsistent data issues.
The HttpApplicationState class provides two methods of lock and unlock to resolve access synchronization problems for application objects, allowing only one thread to access the application state variable at a time.

About locking and unlocking

Lock: Application.Lock ()
Access: Application ("key name") = value
Unlocking: Application.UnLock ()
Note: The lock method and the Unlock method should be used in pairs.
can be used for Web site visitors, chat rooms and other equipment

3. Using the Application event

In a asp.net application, you can include a special optional file-global.asax file, also known as a asp.net application file, that contains code to respond to application-level events raised by ASP.net or HTTP modules.

Third, use Application statistics website visit

Suppose we want to use application to count the access to the site.

• Number of page clicks. Page is clicked +1 times, whether or not the same user clicks the page more than once.

• Number of user visits. Came a user +1, a user to open multiple pages will not affect this number.

We first need to initialize two variables in the Application_Start.

Copy Code code as follows:

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 based on the session, so you can add this variable when Session_Start:

Copy Code code as follows:

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

We see that the application is similar to the session. The only thing to note is that the scope of the application is the entire application, there may be many users at the same time access to application cause concurrency chaos, so in the modification of the application need to lock the application first, after the modification is completed and then unlocked.

The number of page clicks is modified when the page is Page_Load.

Copy Code code as follows:

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 ("User access number:{0}<br/>", application["Uservisit"));
}
}

Iv. Summary of Application

In ASP.net 2.0, application has become less important. Because application has a very weak self-management function, it does not have a time-out mechanism similar to the session. That is, the data in application can only be freed by manual deletion or modification, and the content in application will not disappear as long as the application does not stop. In the next section, we'll see that you can use cache to implement functions like application, while Cache has a rich and powerful self-management mechanism.

Let's summarize the characteristics of application.

• The physical location of the storage. Server memory.

• Type restrictions for storage. Any type.

• The scope of state use. The entire application.

• Storage size limit. Any size.

• life cycle. The application is created at the start (exactly when the user requests a URL for the first time) and is destroyed at the end of the application.

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

• Advantages and disadvantages and precautions. Data retrieval is fast, but the lack of self-management is not automatically released.

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.