ASP. NET status management (Application)

Source: Internet
Author: User

 

ASP. NET allows you to use applicationsProgramStatus to save the value of each active web application. The application status is an instance of the httpapplicationstate class.
Application Status is a global storage mechanism that can be accessed from all pages in a web application. Therefore, the application status can be used to store information that needs to be maintained between server round-trip and page requests.

The application status is stored in a key/value dictionary. This dictionary is created every time a specific URL is requested. You can add application-specific information to this structure to store it during page requests.

Once the application-specific information is added to the application state, the server manages the object.

The application status is the data repository that can be used for all classes in ASP. NET applications.
The application status is stored in the server's memory and faster than storing and retrieving data in the database.
Different from the session Status specific to a single user session, the application status is applied to all users and sessions.
Therefore, the application status is suitable for storing frequently used data that is small and does not change with users.

The application status is stored in the httpapplicationstate class. When you first access the URL Resources in the application, a new instance of this type is created.
The httpapplicationstate class is made public through the Application attribute.

 

How to: Read values from Application Status
Data stored in the application status is converted into objects. Therefore, even if you do not need to serialize the data stored in the application state, you must forcibly convert the data to the corresponding type during data retrieval.

Determine whether the application variable exists and convert it to the corresponding type when accessing the variable.
The followingCodeThis example retrieves the appstarttime value of the application state and converts it to a datetime-type variable named appstatetime.
If (application ["appstarttime"]! = NULL)
{
Datetime myappstarttime = (datetime) application ["appstarttime"];
}

 

How to: Save values in Application Status
Because the application state is stored in the server's memory, a large amount of data in the application state can rapidly fill the server memory. If you restart the application, the application state data will be lost. The application status cannot be shared among multiple servers in the network farm or between auxiliary processes in the network farm. Note that the Application State adopts the free thread mode. Therefore, any data stored in the application state must have built-in synchronization support.

Write value to Application Status
Set the value of the variable in the httpapplicationstate class in the application.
The following code example shows how to set the message variable of an application to a string.
Application ["message"] = "welcome to the contoso site .";

When the application starts, the value is written to the application status.
In the application_start handler of the application global. asax file, set the value of the application state variable. Like the regular. ASPX page, the httpapplicationstate class is also made public through the Application object.
The following code example shows how to set the message variable of the application to a string and initialize the variable pagerequestcount to 0.
Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts
Application ["message"] = "welcome to the contoso site .";
Application ["pagerequestcount"] = 0;
}

Write the value to the application status using the locking method
In the code for setting Application variables, call system. web. httpapplicationstate. lock method, set the application status value, and then call system. web. httpapplicationstate. the unlock method unlocks the application status and releases the application status for other write requests.
The following code example shows how to lock and unlock the application status. This Code increases the pagerequestcount variable value by 1 and then unlocks the application status.
Application. Lock ();
Application ["pagerequestcount"] = (INT) application ["pagerequestcount"]) + 1;
Application. Unlock ();

precautions for Application Status
1. resource
because the application status is stored in the memory, it is faster than saving data to the disk or database. However, storing large data blocks in the application state may exhaust the server memory, which causes the server to paging the memory to the disk. In addition to application status, you can also use ASP. NET cache to store a large amount of application data. ASP. NET cache also stores data in the memory, so the speed is very fast. However, ASP. NET will actively manage the cache and remove items if the memory is insufficient.
2. volatile
because the application state is stored in the server memory, the application state is lost whenever the application is stopped or restarted. For example, if the web. in the config file, restart the application. Unless the application status value is written to a non-volatile storage media (such as a database), the status of all applications will be lost.
3. scalability
the application status cannot be shared among multiple servers serving the same application (such as in the network farm, it cannot be shared among multiple auxiliary processes (such as in a network Garden) serving the same application on the same server. Therefore, applications cannot rely on application states to implement the same application state data on different servers or processes. If an application is running in a multi-processor or multi-server environment, consider using more scalable options (such as databases) for data that must be accurately stored in the application ).
4. Concurrency
the application state adopts the free-threaded mode, that is, the application state data can be accessed by multiple threads at the same time. Therefore, you must ensure that the application state data is updated in a thread-safe manner through built-in synchronization support. You can use the lock and Unlock methods to ensure data integrity. The method is to lock the data so that it can only be written by one source at a time. You can also initialize the application status value in the application_start method in the global. asax file to reduce the possibility of concurrent problems.

Summary
ASP. NET uses the httpapplicationstatefrlrfsystemwebhttpapplicationstateclasstopic class to provide the application status as a method to store the specific information of the global application (visible to the entire application. The application state variable is actually a global variable for ASP. NET applications.
You can store application-specific values in the application status, and the application status will be managed by the server.
Data shared by multiple sessions and infrequently changed is the ideal data to be inserted into application state variables.

Advantages of using Application Status
1. Simple implementation
The application status is easy to use, familiar to ASP developers, and consistent with other. NET Framework classes.
2. Application Scope
Because the application status can be accessed by all pages in the application, storing information in the application status may mean that only one copy of the information is retained (for example, compared to multiple copies that save information in the session status or on a separate page ).

Disadvantages of using Application Status
1. Application Scope
The range of application status may also be a disadvantage. Variables stored in the application state are global only for specific processes in which the application is running, and each application process may have different values. Therefore, you cannot rely on the application status to store unique values or update global counters in the Web farm and web garden server configurations.
2. Limited Data continuity
Because the global data stored in the application state is easy to lose, if the Web server process containing the data is damaged (for example, due to server crash, upgrade, or shutdown ), the data will be lost.
3. Resource requirements
The Application State requires the server memory, which may affect the server performance and scalability of the application.

The well-designed and implemented application status can improve the web application performance. For example, if you place common and related static datasets in the application state, you can improve the site performance by reducing the total number of data requests to the database. However, there is a performance balance here. When the server load increases, application state variables containing large pieces of information will reduce the performance of the Web server. Before removing or replacing a value, the memory occupied by the variables stored in the application state will not be released. Therefore, it is best to use only application state variables for small datasets that are not frequently changed.

 

 

SQL server2005 Transact-SQL new weapon learning Summary-Summary
Ms SQL database backup and recovery stored procedures (enhanced)
SQL Server Distributed Query essay (sp_addmediaserver) and remote login ing (sp_addmediasrvlogin) use small summary)
WAP development data station (latest update)
Custom Format String (implementation of the three interfaces of iformattable, iformatprovider, and icustomformatter)
Asynchronous programming of mcad learning notes (asynccallback delegation, iasyncresult interface, begininvoke method, and endinvoke method)
Mcad learning notes: Calling class methods through reflection, attention, fields, indexers (2 methods)
Serialization of mcad learning notes (binary and soap serialization)
Delegated re-understanding of mcad learning notes (discussion of Delegate constructor, begininvoke, endinvoke, and invoke4 methods)
Winform development, form display, and form value passing knowledge
Microsoft Windows service using mcad Study Notes
Copy all the objects and files under a certain category to the target category (number of objects)
ASP. NET status management (Summary)

 

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.