2 February 2014

In-Process Session

      The InProc Session State Mode is the default Session State Mode. We can host multiple websites/web applications on a single IIS. Each application runs in a separate Application Domain. The InProc Session State Mode stores session data in a memory object in the application worker process (aspnet_wp.exe) in the application domain.

       It is usually the fastest, but more session data means more memory is used on the web server, and that can affect performance. 

        The session data is stored in the application domain of the web server. When the server restarts then existing data is lost. If you modify the Global.asax file and the Web.Config file for an ASP.NET application then the application will be restarted and all the session data will be lost.

         ASP.NET provides two events that help you manage user sessions. These events are defined in the Global.asax file of the web application.

Events in InProc Session:

Session_start(): This  Event Occurs when a new session begins
Session_End(): This event occurs when a session is abandoned or expires.

Implementation of InProc Session

       The InProc Session State Mode is the default session mode but you can also set the Session State Mode and session timeout in the Web.Config file of you application as in the following code snippet.

 <Configuration>
  <system.web>    
    <sessionState mode="InProc" timeout="25"></sessionState>
  </system.web>
</configuration>

     The preceding session timeout setting keeps the session alive for 25 minutes. If you don't define a timeout attribute in the SessionState then the default value is 20 minutes.

Storing Session Value:    

       Now provide the following code in the button click event to store the user name in a session variable in the code behind file ("User.aspx.cs") and redirects to another page to show the session data. The following is the code snippet for it.

using System;
public partial class User : System.Web.UI.Page
 {
    protected void btnSubmit_Click(object sender, EventArgs e)
     {
       Session["Name"] = txtName.Text;
        Response.Redirect ("UserDetail.aspx");
       }
  }


Retrieving Session Value:

        To retrieve session data I create another web page "UserDetail.aspx" and its code behind page, the Page_Load () event has code to retrieve the session data. As in the following code snippet if the session data exists then the try block is successfully executed otherwise the catch block will be executed.

    using System;
     public partial class UserDetail : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
           try
           {
            string name = Session["Name"].ToString();
            Response.Write(string.Format("User name is :{0}", name));
           }
          catch (NullReferenceException ex)
          {
            Response.Write(ex.Message +" Because Session doesn't exist");
          }
    }
}

No comments:

Post a Comment