Online Book Reader

Home Category

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

By Root 5611 0
when it begins to handle the EndRequest event.

GetItem

Returns the session item matching the specified ID from the data store. The session item selected is locked for read. The method serves requests from applications that use a read-only session state.

GetItemExclusive

Returns the session item matching the specified ID from the data store and locks it for writing. It’s used for requests originated by applications that use a read-write session state.

Initialize

Inherited from the base provider class, performs one-off initialization.

InitializeRequest

Called by the default session state module when it begins to handle the AcquireRequestState event.

ReleaseItemExclusive

Unlocks a session item that was previously locked by a call to the GetItemExclusive method.

RemoveItem

Removes a session item from the data store. It’s called when a session ends or is abandoned.

ResetItemTimeout

Resets the expiration time of a session item. It’s invoked when the application has session support disabled.

SetAndReleaseItemExclusive

Writes a session item to the data store.

SetItemExpireCallback

The default module calls this method to notify the data store class that the caller has registered a Session_End handler.

Classes that inherit the SessionStateStoreProviderBase class work with the default ASP.NET session state module and replace only the part of it that handles session-state data storage and retrieval. Nothing else in the session functionality changes.

Locking and Expiration


Can two requests for the same session occur concurrently? You bet. Requests can certainly arrive in parallel—for example, from two frames or when a user works with two instances of the same browser, the second of which is opened as a new window. To avoid problems, a state provider must implement a locking mechanism to serialize access to a session. The session state module determines whether the request requires read-only or read-write access to the session state and calls GetItem or GetItemExclusive accordingly. In the implementation of these methods, the provider’s author should create a reader/writer lock mechanism to allow multiple concurrent reads but prevent writing on locked sessions.

Another issue relates to letting the session state module know when a given session has expired. The session state module calls the method SetItemExpireCallback when there’s a Session_End handler defined in global.asax. Through the method, the state provider receives a callback function with the following prototype:

public delegate void SessionStateItemExpireCallback(

string sessionID, SessionStateStoreData item);

It has to store that delegate internally and invoke it whenever the given session times out. Supporting expiration callbacks is optional and, in fact, only the InProc provider actually supports it. If your custom provider is not willing to support expiration callbacks, you should instruct the SetItemExpireCallback method to return false.

Note

A provider that intends to support cookieless sessions must also implement the CreateUninitialized method to write a blank session item to the data store. More precisely, a blank session item is an item that is complete in every way except that it contains no session data. In other words, the session item should contain the session ID, creation date, and perhaps lock IDs, but no data. ASP.NET generates a new ID (in cookieless mode only) whenever a request is made for an expired session. The session state module generates the new session ID and redirects the browser. Without an uninitialized session item marked with a newly generated ID, the new request will again be recognized as a request for an expired session.

Replacing the Session Data Dictionary


SessionStateStoreData is the class that represents the session item—that is, a data structure that contains all the data that is relevant to the session. GetItem and GetItemExclusive, in fact, are defined to return an instance of this class. The class has three properties: Items, StaticObjects, and Timeout.

Items indicates

Return Main Page Previous Page Next Page

®Online Book Reader