Server-Based Technique #1: Application State
Now that you understand the client-based state management techniques let's explore the server-based state management techniques. We'll begin with application state.
|You must use relative path references when you use cookieless sessions.|
I'll start by introducing you to application state variables. They are similar to global variables in traditional Windows applications because they are available from anywhere in the application. What makes them different from Windows application global variables is that they are available to every browser session running the application. You can think of application state variables as a non-existent type of global variable in Windows applications that is available to not only the current user, but to all users running the application. Typically, application state variables contain fairly static data that multiple sessions will share.
You add values to the application state with code like this.
Application("ApplicationName") = "My
Application("ApplicationStart") = Now"
You can add objects of any type to application state. You can retrieve application state variables like this.
Dim AppName As String =
And you use code like this to remove application state variables.
You can remove all the variables from application state by using the RemoveAll
Because application state variables are shared across the entire application, there is a possibility that multiple users could attempt to update the same application state variable at the same time. This would not be a good thing. Fortunately Microsoft provided the Lock
methods to help get around this problem.
method locks the application state object so that only a single user can update the data. It makes sense to conclude that Unlock
releases the Lock
, which it does.
Application("PagesDisplayed") += 1
You need to understand that an application session starts when the first person accesses the application. In other words, the application does not run if no one visits the site. Once that first visitor shows up and launches the home page for the first time, the application starts.
|An application session starts when the first person accesses the application.|
There are a number of events related to the Application object contained in the global.asax
file. Discussing these events is beyond the scope of this article but I suggest you take a look at them and understand what they do and when they are raised.