This blog will explain different Session Modes in ASP.NET. ASP.NET offers various session modes depending on the storage option for session data. Here I will explain different Session Modes available in ASP.NET.

Session Modes in ASP.NET:

ASP.NET provides various Session Modes for storage of Session Data. Here is the different session modes available in ASP.NET.

  • InProc mode: This is the default Session State modes which store the session data in memory on the web server.
  • StateServer (Out-Proc) mode: This stores the session data in separate memory called the ASP.NET Service. This mode ensures that the session data preserves when the Application Process restarts.
  • SQL Server mode: In this mode session data is stored in the SQL Server Database. This mode also ensures that the session data preserves when the Web Application restarted. Also, this mode makes session data available to several Web Server.
  • Custom mode: This mode enables you to specify the custom storage option.
  • Off mode: This mode disables the session state. This increases the performance of the application.

Session Modes

InProc Mode:

InProc is the default session state mode. The InProc mode stores the session state in the memory of the web server. We can host the multiple websites on the IIS. Each application runs in the separate application domain. The InProc stores the session state in the memory object in the application. As it stores the data in the same application domain in which the application exists so it becomes easier to store and retrieve data from the session. This makes it faster as compared to another mode, but more session data means more memory on the web server and this can cause a major impact on the performance. Since the data is stored in the same application domain of the web server so when the server restarts then existing data is lost. This data can also be caused by the change made in the Global.asax and
web.config file.

Since this is the default mode for session state but you can also specify the session state using the following syntax in the web.config file.

<sessionState mode="InProc" timeout="25"></sessionState>

StateServer Mode:

The StateServer Session mode is also known as Out-Proc session mode. In StateServer session mode the data is stored in the sperate server as a stand-alone Window Service which is independent of IIS. The State server could be located on the same server or on the different server. This State Server mode has several drawbacks like It has the overhead of serialization and de-serialization. Since in state server session mode the session data exists on the different server so when the user tries to retrieve the session, our application hits a different process this will increase the cost of data access.
To configure an ASP.NET application to use StateServer mode, in the application’s Web.config file do the following:

  • Set the mode attribute of the sessionState element to StateServer.
  • Set the stateConnectionString attribute to tcpip=serverName:42424.
<configuration>
<system.web>
<sessionState mode="StateServer"
stateConnectionString="tcpip=SampleStateServer:42424"
cookieless="false"
timeout="20"/>
</system.web>
</configuration>

SQL Server Mode:

In SQL Server Session Mode data is stored in the SQL Server Database. This is similar to State Server mode expect that the session data is stored in a SQL Server Database Like State Server SQL Server also Independent of Application Domain. This means SQL Server mode stores the session data outside the application. So this mode also overcomes the problem that exists inProc. This mode is slower than State Server Session mode but can be shared between several web servers but this mode is more reliable. This mode also ensures that the Session Data preserves if the application restarts.

To configure an ASP.NET application to use SQL Server mode, in the application’s Web.config file do the following:

  • Set the mode attribute of the sessionState element to SQLServer.
  • Set the sqlConnectionString attribute to a connection string for your SQL Server database.
<configuration>
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Integrated Security=SSPI;data 
source=SampleSqlServer;" />
</system.web>
</configuration>

Conclusion:

I hope you loved this post about different Session Modes in ASP.NET. This is also mostly asked Interview Question. I would like to have your feedback and suggestion in the comment box.

Thank You.

Leave a Comment