Emailid
Password
         
  
    Forgot password

New user Sign Up
 

ASP.NET execution

       Current Rating:  20%                                                     Total Members Rated:  1
                                                                     Send To Friend

  

ASP.NET execution

 

 

The System.Web.UI.Page class in ASP.NET is the origin for Web page. The Page class aggregates an instance of the HttpSession object for session data. In this example, a custom Page class called SessionPage is derived from the System.Web.UI.Page to offer all the same features as the Page class. The only difference with the derived page is that the default HttpSession is overridden with a custom session object. 

 

 

 

public class SessionPage : System.Web.UI.Page { ... public new mySession Session = null; ... }

 

 

 The custom session class will limit the session data type to be string only for interoperability with the classic ASP. The custom session class is responsible for storing the session state in memory using the Hybrid Dictionary object.

 

 

 [Serializable] public class mySession { private HybridDictionary dic = new HybridDictionary(); public mySession() { } public string this [string name] { get { return (string)dic[name.ToLower()]; } set { dic[name.ToLower()] = value; } } }

 

 

The OnInit method is used to set the initialize state of the Page object. If the request does not have the mySession cookie, a new mySession cookie will be issued to the requester. The Page class exposes different events and methods for customization. Otherwise, the session data will be retrieved from SQL Server using a custom data access object, SessionPersistence. The dsn and SessionExpiration values are retrieved from the web.config.

 

 

override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { cookie = this.Request.Cookies[sessionPersistence.SessionID]; if (cookie == null) { Session = new mySession(); CreateNewSessionCookie(); IsNewSession = true; } else Session = sessionPersistence.LoadSession( Server.UrlDecode(cookie.Value).ToLower().Trim(), dsn, SessionExpiration ); this.Unload += new EventHandler(this.PersistSession); } private void CreateNewSessionCookie() { cookie = new HttpCookie(sessionPersistence.SessionID, sessionPersistence.GenerateKey()); this.Response.Cookies.Add(cookie); }

 

 

The BinaryFormatter of the Microsoft .NET Framework is used by the SessionPersistence class to serialize and deserialize the session state in binary format for optimal performance. The resulting binary session state data can then be stored in the SQL Server as an image field type.

 

 

public mySession LoadSession(string key, string dsn, int SessionExpiration) { SqlConnection conn = new SqlConnection(dsn); SqlCommand LoadCmd = new SqlCommand(); LoadCmd.CommandText = command; LoadCmd.Connection = conn; SqlDataReader reader = null; mySession Session = null; try { LoadCmd.Parameters.Add("@ID", new Guid(key)); conn.Open(); reader = LoadCmd.ExecuteReader(); if (reader.Read()) { DateTime LastAccessed = reader.GetDateTime(1).AddMinutes(SessionExpiration); if (LastAccessed >= DateTime.Now) Session = Deserialize((Byte[])reader["Data"]); } } finally { if (reader != null) reader.Close(); if (conn != null) conn.Close(); } return Session; } private mySession Deserialize(Byte[] state) { if (state == null) return null; mySession Session = null; Stream stream = null; try { stream = new MemoryStream(); stream.Write(state, 0, state.Length); stream.Position = 0; IFormatter formatter = new BinaryFormatter(); Session = (mySession)formatter.Deserialize(stream); } finally { if (stream != null) stream.Close(); } return Session; }

 

 

The Page class Unload event is fired, and an event handler registered with the Unload event will serialize the session data into binary format and save the resulting binary data into SQL Server.

 

 

private void PersistSession(Object obj, System.EventArgs arg) { sessionPersistence.SaveSession( Server.UrlDecode(cookie.Value).ToLower().Trim(), dsn, Session, IsNewSession); } public void SaveSession(string key, string dsn, mySession Session, bool IsNewSession) { SqlConnection conn = new SqlConnection(dsn); SqlCommand SaveCmd = new SqlCommand(); SaveCmd.Connection = conn; try { if (IsNewSession) SaveCmd.CommandText = InsertStatement; else SaveCmd.CommandText = UpdateStatement; SaveCmd.Parameters.Add("@ID", new Guid(key)); SaveCmd.Parameters.Add("@Data", Serialize(Session)); SaveCmd.Parameters.Add("@LastAccessed", DateTime.Now.ToString()); conn.Open(); SaveCmd.ExecuteNonQuery(); } finally { if (conn != null) conn.Close(); } } private Byte[] Serialize(mySession Session) { if (Session == null) return null; Stream stream = null; Byte[] state = null; try { IFormatter formatter = new BinaryFormatter(); stream = new MemoryStream(); formatter.Serialize(stream, Session); state = new Byte[stream.Length]; stream.Position = 0; stream.Read(state, 0, (int)stream.Length); stream.Close(); } finally { if (stream != null) stream.Close(); } return state; }

 

 

In a new ASP.NET project, a reference will be made to the SessionUtility assembly, and every page will derive from the SessionPage instead of from the Page class in order to share session with classic ASP codes. Once the porting is completed, the new application can switch back to use the native HttpSession object by commenting out the Session variable declaration in the SessionPage class to unhide the base HttpSession. The SessionPage class and its associated classes are packaged in the SessionUtility assembly.

 

Happy programing!


                           Rate This Article:   

Author is Offline
  Author: Zebastti Tottileit
       


Comments Posted
Label
Subject Author Status Date

Congrats!!!

Ausef Machaino 26/06/2008

Uptodate!

Ausef Machaino 26/06/2008

All the best!

Boroto Meida 04/07/2008

 

Post Comment

Related Articles
N-Tier in ASP.NET or other .NET apps
Special Features of ASP.NET
Classic ASP and ASP.NET
Demo Program in classic ASP and ASP.NET.
Data Grid ASP.net 1.1 and JavaScript



Home | About Us | Site Map | Privacy Policy