Asp. NET the use of Application objects (interview questions)

Source: Internet
Author: User
Tags server memory

Asp. The use of application objects in net

This article is guided by:application object is An instance of the HttpApplicationState class,application The state is global to the entire application. the application Object stores a small amount of data in server memory that is separate from the user's request. Because of its very fast access and as long as the application does not stop and the data persists, we usually Initialize some data at Application_Start, which can be accessed and retrieved quickly in future accesses.

I. understanding of Application objects

The purpose of the Application object in the actual network development is to record the information of the whole network, such as the number of people on the line, the online list, the opinion survey and the online election. Share information among many users of a given application and persist data for the duration of the server run. the application object also has a way to control access to application-tier data and events that can be used to trigger the process when the application starts and stops.

Global.asax(usually we do not rename) is an event that is used to handle the global application. Open the file, the system has defined some of the event handling methods for us.

void Application_Start (object sender, EventArgs e)

{

code to run when the application starts

}

void Application_End (object sender, EventArgs e)

{

code to run when the application shuts down

}

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 is started

}

void Session_End (object sender, EventArgs e)

{

code to run at the end of the session

Note : only the sessionstate mode in the Web. config file is set to the session_end event is only raised when InProc

if the session mode is set to stateserver or SQL Server, the event is not raised

}

With these annotations we can see that these events are the events of the entire application and are not related to a particular page.

Second, The application of Application object

1. Saving information using application objects

(1), using 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. there may be situations where multiple users can 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 offers two methods of Lock and Unlocktoaddress the application object, 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

Unlock:application.unlock ()

Note: TheLock method and the UnLock method should be used in pairs.

Can be used for website visitors, chat rooms and other equipment

3. using the application event

In an ASP . NET application, you can include a special optional file-theGlobal.asax file, also called an asp .  It contains code that responds to application-level events raised by the ASP. NET or HTTP module.

Iii. Summary ofapplication

InASP. 2.0in whichApplicationhas become not very important. BecauseApplicationthe self-management function is very weak, it has no similarSessiontime-out mechanism. In other words,Applicationonly manually deleted or modified to free memory, as long as the application does not stop,Applicationwill not disappear from the content. In the next section, we'll see that you can use theCacheachieve similarApplicationthe function, whileCacheThere is also a rich and powerful self-management mechanism.

Summarize the features of application :

· The physical location of the storage. server memory. • The type limit of the storage. Any type.

· The range used by the state. The entire application. The size limit of the storage. Any size.

· Life cycle. When the application starts, it is created (exactly when the user first requests a URL ) and is destroyed at the end of the application.

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

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

Asp. NET the use of Application objects (interview questions)

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.