InProc Session State Mode in ASP.Net

Introduction

This article explains the "InProc" Session State Mode in ASP.NET.

ASP.NET supports various Session State Modes depending on various storage options for session data. The following are the available Session State Modes in ASP.NET:

  • InProc
  • StateServer
  • SQLServer
  • Custom
  • Off

Each option is identified by a value in the SessionStateMode enumeration. You can visit my previous article to learn about sessions including the basics of sessions.

Introduction to ASP.NET Session

InProc Session State Mode

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.
 
Sr. NoEventCall
1Session_Start()This event occurs when a new session begins.
2Session_End()This event occurs when a session is abandoned or expires.

Table 1.1 Session Events

The InProc Session State Mode is the only Session State Mode that supports the Session_End() event. Figure 1.1 shows the general workflow for session events in the InProc Session State Mode.

Session State Mode

Figure 1.1 General work flows for InProc Session State Mode

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.

  1. <configuration>  
  2.   <system.web>      
  3.     <sessionState mode="InProc" timeout="25"></sessionState>  
  4.   </system.web>  
  5. </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 and retrieving session data values


I will create a Web Application using Visual Studio 2012. So let's see the procedure for creating a Web application.

Step 1: Go to "File" -> "New" -> "Website...".

Step 2: Choose "ASP.NET Empty Web Site" from the list, then choose Web location "HTTP" from the Web location list then fill in the web site path with the name " http://localhost/InProcSessionExample".

Web Site on Visual Studio

Figure 1.2 Create a new Web Site on Visual Studio

This website is created in the "C:\inetpub\wwwroot\InProcSessionExample" location because this site is on IIS.

Website on IIS

Figure 1.3 Website on IIS

Step 3: Start Web Site on IIS

Right-click on "Default Web Site" then go to "Manage Web Site" then click on "Start".

Start web site on IIS

Figure 1.4 Start web site on IIS

Step 4: Store Session Data

Now create a web page that stores data in a session variable. I create a web form "User.aspx" that has one TextBox control for the user name input and one button control that stores the user name in a session variable after a click. The following code snippet is for "User.aspx":

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs" Inherits="User" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.    Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />  
  11.         <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </html>  
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. 
  1. using System;  
  2. public partial class User : System.Web.UI.Page  
  3. {  
  4.     protected void btnSubmit_Click(object sender, EventArgs e)  
  5.     {  
  6.         Session["Name"] = txtName.Text;  
  7.         Response.Redirect("UserDetail.aspx");  
  8.     }  
  9. }   
 
Step 5: Retrieve Session Data

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.

  1. using System;  
  2. public partial class UserDetail : System.Web.UI.Page  
  3. {  
  4.     protected void Page_Load(object sender, EventArgs e)  
  5.     {  
  6.         try  
  7.         {  
  8.             string name = Session["Name"].ToString();  
  9.             Response.Write(string.Format("User name is :{0}", name));  
  10.         }  
  11.         catch (NullReferenceException ex)  
  12.         {  
  13.             Response.Write(ex.Message +" Becasue Session doesn't exist");  
  14.         }  
  15.     }  
  16. }  

Step 6: Run the application

You fill in the user name in the "Name" input field then click on the "Submit" button. Then you get the result from your first page of input data that is successfully shown on another page. Let's see that in Figure 1.5.

ASP.NET Aplication

Figure 1.5 Output of application

Now reset IIS means runs "iisreset" command.

IIS reset

Figure 1.6 IIS reset

Now refresh the page where the session data is shown, the UserDetail.aspx page and you will see that now the catch block code will be executed because the session data is stored on the server so that data is lost by server reset.

Session data lost on IIS reset

Figure 1.7 Session data lost on server reset

Advantages of InProc Session State Mode

Here is a list of the advantages of the InProc Session State Mode.
  1. It is easy to implement.
  2. It stores the session data on the server so it is fast.
  3. In this mode, it is not necessary to serialize and de-serialize to store and retrieve data in the session variables.

Disadvantages of InProc Session State Mode

Here is a list of the disadvantages of the InProc Session State Mode.

  1. When the application domain or worker process recycles, the session data will be lost.
  2. It is usually the fastest, but more session data means more memory is used on the web server, and that can affect performance
  3. It won't work in web farm scenarios and web garden scenarios, because in these scenarios multiple "aspnet_wp.exe" processes will be running on the same machine.

Conclusion

The InProc Session State Mode is a very fast session storing mechanism but suitable only for small web applications. InProc session data would be lost if we restart the server or if the application domain is recycled.

Up Next
    Ebook Download
    View all
    Learn
    View all