Asp. NET state Management eight (application application)

Source: Internet
Author: User
Tags datetime server memory

asp.net allows you to use application state to hold values for each active Web application, and application state is an instance of the HttpApplicationState class.

Application state is a global storage mechanism that can be accessed from all pages in a WEB application. Therefore, application state can be used to store information that needs to be maintained between server round trips and between page requests.

Application state is stored in a key/value dictionary, which is created each time a specific URL is requested. Application-specific information can be added 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.

Application state is a data repository that can be used to asp.net all classes in the application.

Application state is stored in the server's memory and is faster than storing and retrieving data in the database.

Unlike session state, which is specific to a single user session, application state applies to all users and sessions.

Therefore, application state is ideal for storing commonly used data that is small in number and does not change with the user.

Application state is stored in the HttpApplicationState class, and a new instance of the class is created the first time a user accesses a URL resource in the application.

The HttpApplicationState class is exposed through the application property.

How to: Read values from application state

Application state stores data that is typed as Object. Therefore, even if data is stored in application state without being serialized, it must be cast to the appropriate type when the data is retrieved.

Determine if the application variable exists, and then convert it to the appropriate type when accessing the variable.

The following code example retrieves the application state value Appstarttime and converts it to a DateTime-type, named Appstatetime variable.

if (application["appstarttime"]!= null)
{
DateTime myappstarttime = (DateTime) application["Appstarttime"];< c3/>}

How to: Save values in application state

Because application state is stored in the server's memory, large amounts of data in application state can quickly populate server memory. If you restart the application, the application state data is lost. Application state cannot be shared across multiple servers in a network farm or between worker processes in a network garden. Finally, note that application state takes free threading mode, so any data stored in application state must have built-in synchronization support.

Write value to application state

Set the value of a variable in the HttpApplicationState class in your application.

The following code example shows how to set the application variable message to a string.

application[' message ' = ' Welcome to the Contoso site. '

Write values to application state when the application starts

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.