The application status is a data repository that can be used by all classes in ASP. NET applications. It is stored in the memory of the server, so it is faster than storing and retrieving information 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 state is used to store frequently used data that is small and does not change with the user.
1. Use Application Status
The application status is stored in the instance of the httpapplicationstate class. The key-value Dictionary of the public object.
When you first access any URL resource in the application, an httpapplicationstate instance is created. The httpapplicationstate class is usually accessed through the Application attribute of the httpcontext class.
You can use the application status in two ways. You can add, access, or remove values in the contents set using code. When the application is running, you can access the httpapplicationstate class at any time. However, this class is usually used to load application state data when an application starts. To this end, you can add the code for loading the application status to the application_start method in the global. asax file.
In addition, you can add an object to the staticobjects set through the <object runat = "server"> declaration in the global. asax file of the Web application. Then, you can access the application state defined in this method by code anywhere in the application. The following example demonstrates the object Declaration of the application state value:
<Object runat = "server" Scope = "application" id = "myinfo"
Progid = "mswc. myinfo">
</Object>
You can only add objects to the staticobjects set in the global. asax file. If you try to add objects directly through code, this set will cause notsupportedexception.
You can access the object members stored in the application state without referencing the application set. The following code example demonstrates how to reference object members defined in the staticobjects set in the application state:
Protected void page_load (Object sender, eventargs E)
Label1.text = myinfo. title;
End sub
2. Precautions for Application Status
Note the following when using the application status:
· Resources are stored in the memory, which is faster than storing data in disks or databases. 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.
· Volatility because the application status is stored in the server memory, the application status will be 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.
· The Scaling 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 ).
· Concurrent application state adopts the free thread mode, that is, 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.