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. No | Event | Call |
1 | Session_Start() | This event occurs when a new session begins. |
2 | Session_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.
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.
- <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 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".
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.
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".
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":
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs" Inherits="User" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
- </div>
- </form>
- </body>
- </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.
- 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");
- }
- }
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.
- 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 +" Becasue Session doesn't exist");
- }
- }
- }
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.
Figure 1.5 Output of application
Now reset IIS means runs "iisreset" command
.
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.
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.
- It is easy to implement.
- It stores the session data on the server so it is fast.
- 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.
- When the application domain or worker process recycles, the session data will be lost.
- It is usually the fastest, but more session data means more memory is used on the web server, and that can affect performance
- 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.