In the previous chapter, we discussed three techniques for managing client state: session, view state, and HTTP cookies. In this chapter, we’ll examine four techniques for managing application state and request state: cache, static variables, application, and request. These are described in Table 6-1.
Type of State | Applies To | Description |
---|---|---|
Cache | Application | State stored within the application’s memory and available anywhere within the application. Cache additionally supports dependencies and other features to expire items from memory. |
Static variable | Application | Declared in global.asax or from an HttpModule and available anywhere within the application. |
Application | Application | State stored within the application’s memory and available anywhere within the application. |
HttpContext | Request | State stored within HttpContext is accessible only for the duration of the request. |
Application state describes any data or state that is shared throughout the application using the Application API, the Cache API, or static application variables. Request state describes any data or state that is shared throughout the duration of the request, that is, created once and then used multiple times throughout the lifetime of the request.
Some developers follow the line of thinking that all Web applications should be stateless. Although statelessness is a noble concept, it is unrealistic. In fact, HttpRuntime—the underlying plumbing that runs ASP.NET—is not even stateless. When the first request comes into an ASP.NET application, the HttpRuntime performs multiple tasks, one of which is parsing and storing the configuration for the application from the web.config/machine.config configuration files. The resulting configuration data gleaned from these files is then stored in the ASP.NET cache (the cache is specialized in-process memory) to alleviate the need to perform multiple requests to the file system. In this chapter, we’ll discuss how to use these techniques to improve the scalability and performance of your application.