Share via


How to: Save Values in Application State

Application state is a data repository that is available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

Application state is stored in the HttpApplicationState class, a new instance of which is created the first time a user accesses any URL resource in an application. The HttpApplicationState class is exposed through the Application property.

Application state stores data as Object data types. Therefore, you must convert the data back to the appropriate type when retrieving it.

Application state is stored in memory on the server, so a large amount of data in application state can fill up server memory quickly. If the application is restarted, application state data is lost. Application state is not shared between multiple servers within a Web farm or between worker processes in a Web garden. Finally, application state is free-threaded, so any data that is stored in application state must have built-in synchronization support. For more information about these considerations, see ASP.NET Application State Overview and ASP.NET State Management Recommendations.

To write a value to application state

  • In your application, set the value of the variable in the HttpApplicationState class.

    The following code example shows how you can set the application variable Message to a string.

    Application("Message") = "Welcome to the Contoso site."
    
    Application["Message"] = "Welcome to the Contoso site.";
    

To write a value to application state when the application starts

  • In Application_Start handler of your application's Global.asax file, set the value of the application state variable. Just as in a regular .aspx page, the HttpApplicationState class is exposed through the Application object.

    The following code example shows how you can set the application variable Message to a string and initialize the variable PageRequestCount to 0.

    Application("Message") = "Welcome to the Contoso site."
    Application("PageRequestCount") = 0
    
    Application["Message"] = "Welcome to the Contoso site.";
    Application["PageRequestCount"] = 0;
    

Writing a Value to Application State with Locking

Application state variables can be accessed by multiple threads at the same time. Therefore, to prevent invalid data, you must lock application state for writing by only one thread before setting values.

Note

You should always modify application state data within a lock statement unless you have set some other type of lock. For more information, see Synchronizing Data for Multithreading.

To write a value to application state with locking

  • In the code where you set the application variable, call the HttpApplicationState.Lock method, set the application state value, and then call the HttpApplicationState.UnLock method to unlock the application state, freeing it for other write requests.

    The following code example shows how you can lock and unlock application state. The code increases the PageRequestCount variable by 1 and then unlocks application state.

    Application.Lock()
    Application("PageRequestCount") = _
        CInt(Application("PageRequestCount")) + 1
    Application.UnLock()
    
    Application.Lock();
    Application["PageRequestCount"] = 
        ((int)Application["PageRequestCount"])+1;
    Application.UnLock();
    

See Also

Tasks

How to: Read Values from Application State

Concepts

application state

ASP.NET State Management Overview

ASP.NET State Management Recommendations