Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [352]

By Root 5558 0
lock on the session state, all other pages are blocked regardless of whether they have to read or write. For example, if two frames attempt to write to Session, one of them has to wait until the other finishes. Figure 17-2 shows the big picture.

Figure 17-2. Page access to the session state is synchronized, and a serialization/deserialization layer ensures that each request is served an up-to-date dictionary of values, stored at the application’s convenience.

Note

Concurrent access to the session state is not very common in reality. It might happen if you have a multiframe page or if your users work with two copies of the same page or multiple pages of the same application at the same time. It also happens when you use session-enabled HTTP handlers to serve embedded resources such as images or cascading style sheet (CSS) files. By default, you are protected against concurrent accesses. However, declaring the exact use of the session state that a page is going to make (read/write, readonly, or no use) is an excellent form of optimization. You do this through the EnableSessionState attribute on the @Page directive.

Properties of the HttpSessionState Class


The HttpSessionState class is defined in the System.Web.SessionState namespace. It is a generic collection class and implements the ICollection interface. The properties of the HttpSessionState class are listed in Table 17-5.

Table 17-5. HttpSessionState Properties

Property

Description

CodePage

Gets or sets the code page identifier for the current session.

Contents

Returns a reference to this object. It’s provided for ASP compatibility.

CookieMode

Details the application’s configuration for cookieless sessions. Declared to be of type HttpCookieMode. (I’ll discuss this in more detail later.)

Count

Gets the number of items currently stored in the session state.

IsCookieless

Indicates whether the session ID is embedded in the URL or stored in an HTTP cookie. It’s more specific than CookieMode.

IsNewSession

Indicates whether the session was created with the current request.

IsReadOnly

Indicates whether the session is read-only. The session is read-only if the EnableSessionState attribute on the @Page directive is set to the keyword ReadOnly.

IsSynchronized

Returns false. (See references to this later in the chapter.)

Item

Indexer property, provides read/write access to a session-state value. The value can be specified either by name or index.

Keys

Gets a collection of the keys of all values stored in the session.

LCID

Gets or sets the locale identifier (LCID) of the current session.

Mode

Gets a value denoting the state client manager being used. Acceptable values are listed in Table 17-4.

SessionID

Gets a string with the ID used to identify the session.

StaticObjects

Gets a collection including all instances of all objects declared in global.asax using an tag with the scope attribute set to Session. Note that you cannot add objects to this collection from within an ASP.NET application—that is, programmatically.

SyncRoot

Returns a reference to this object. (See references to this property later in the chapter.)

Timeout

Gets or sets the minutes that the session module should wait between two successive requests before terminating the session.

The HttpSessionState class is a normal collection class because it implements the ICollection interface, but synchronization-wise it is a very special collection class. As mentioned, the synchronization mechanism is implemented in the SessionStateModule component, which guarantees that at most one thread will ever access the session state. However, because HttpSessionState implements the ICollection interface, it must provide an implementation for both IsSynchronized and SyncRoot. Note that IsSynchronized and SyncRoot are collection-specific properties for synchronization and have nothing to do with the session synchronization discussed previously. They refer to the ability of the collection class (HttpSessionState in this case) to work in a synchronized

®Online Book Reader