Technical Training
ASP.NET 2.0Table of Contents
ASP.NET Customizing the Session State Mechanism
ASP.NET Customizing the Session State Mechanism - Page 2ASP.NET Customizing the Session State Mechanism Page - 2
ASP.NET Customizing the Session State Mechanism
Based on the above script ASP.NET will use the Samples.MyDataStore class to access the session data. This class is loaded into the MyDataStoreLib assembly.
The next aspect that can be customized is the Session State Item. The default session state item is named SessionStateStoreData. A custom class can be inherited from this class to create a custom session data store item.
The third aspect of the default session state module that can be customized is the Session Data Dictionary. The dictionary is a component of the SessionStateStoreData class and contains the user defined items. The Items collection is actually exposed to the page through the Session property. By default it is an instance of the HttpSessionState class. This default dictionary can be replaced by a custom one by creating a class that implements the ISessionSessionItemCollection Interface.
The last aspect of the session state module that can be customized is the generation of the session ID. ASP.NET 2.0 uses the HTTP module SessionIDModule to generate the session ID. It implements the ISessionIDModule interface. This module can be replaced with a custom module. This module should be registered in the configuration file by using the following code:
< httpModules >
< remove name=”SessionID” / >
< add name= “MySessionID” type=”Samples.MyIDModule.MyLib” / >
< / httpModules >
It must be noted that the default session ID Http module will have to be removed before the custom module is added to the configuration file.
Writing a custom session state module
Manipulating an existing default module is easy compared to writing a custom session state module. The developer has to write all the key application events—AcquireRequestState, ReleaseRequestState and EndRequest.
The module will first have to be initialized using the Init and Dispose methods of the IHttpModule. In the Init method the developer will have to read the configuration information from the web.config file and then configure the state of the module and prepare it for handling the custom configuration. The application events are subscribed to in the Init method and a reference to the session Id is obtained. Look at the following code:
ISessionIDModule idModule;
HttpApplication app=Httpcontext.Current.ApplicationInstance;
idModule=SessionStateUtility.GetSessionIDModule(app);
In the above instance the reference to the currently loaded module is obtained.
The AcquireRequestState event helps retrieve the sessionID for the request by calling the GetSessionID method. The developer must take into consideration cookies or implement alternate schemes to store session IDs. If no session Id is found a new id is generated to the Response object by calling SaveSessionID on the ISessionIDModule interface. Once the session ID is retrieved the module connects to the data store and retrieves the associated data. The session state object is now created and bound to the current context and made available to applications through the sessions property. If this is a new session the developer will have to fire the Start event.
Once the session state is to be terminated, the ReleaseRequestState event is to be fired. The session state object should be detached from the HTTP context and saved back into the data store. If the session state is empty a new session will have to be created and this aspect will have to be customized by the developer. Support for Session_OnStart and Session_OnEnd should be created using the name Session and it should be registered in the custom module as such. The following code can be used to replace the session module:
< httpModules >
< remove name= “Session” / >
< add name = “Session” type= “Samples.MySession.MyLib” / >
< / httpModules >
In this section of the tutorial we have studied how to manipulate the default session modules and also create custom session modules. In the next section we will examine the ASP.NET Cache object and its features.
ASP.NET 2.0
- Getting started with ASP.NET 2.0
- .NET Framework Fundamentals
- Microsoft.NET Framework Tools
- Application Development in .NET
- What's New in the .NET Framework 2.0 ?
- Introduction to Visual Studio.NET
- Installing Visual Studio.NET 2005
- Working with Visual Studio.NET Web Applications
- Whats New in ASP.NET 2.0
- Creating an ASP.NET Application
- ASP.NET Code Directory
- ASP.NET Page Object Model
- ASP.NET Server Controls
- ASP.NET Working With Master Pages
- ASP.NET Creating Content for Master Page
- ASP.NET Referencing Master Page Members
- ASP.NET Changing Master Pages Dynamically
- ASP.NET Creating Nested Master Pages
- ASP.NET Working with Web Parts
- ASP.NET Using Web Parts and Controls in Web Pages
- ASP.NET Web Pages and Layout
- ASP.NET - Adding Web Parts at Run Time
- ASP.NET Personalization: User Profiles and Themes
- ASP.NET Data Access features
- ASP.NET State Management
- ASP.NET Customizing the Session State Mechanism
- ASP.NET State Management And Caching
- ASP.NET Security
- Forms Authentication in ASP.NET
- ASP.NET Managing Membership and Roles
- ASP.NET Configuring Page-Level Caching
- ASP.NET Setting Application-Level Caching
- ASP.NET Data Source Object Model
- ASP.NET SqlDataSource Control
- ASP.NET Data Bound Controls
- ASP.NET GridView Control
- ASP.NET GridView Filtering
- ASP.NET Adding Sorting and Paging in GridView
- ASP.NET DataBound Controls - Details View
- ASP.NET Using a Grid to Display Detail Information
- ASP.NET Displaying Master-Detail Data on the Same Page
- Displaying Master-Detail Data on Separate Pages in ASP.NET
- ASP.NET Creating Web Wizards
- ASP.NET : Dynamic Image control
- ASP.NET Advanced Site Functionality







