Please navigate to the bottom of the page for Table of Contents

Saturday, May 28, 2011

ASP.NET Session modes explained

One of the favorite interview questions when interviewing for a web developer jobs is on ASP.NET session state. In fact, the lack of knowledge about session state providers is a very big negative when you are applying for jobs that deal in high traffic sites running on multiple servers or web farms. One of my favorite questions is on what are the different session state providers in ASP.NET?

The answer is quite simple if you have ever added the <sessionState> element in a web.config:

image

ASP.NET session state supports several different storage options for session data. Each option is identified by a value in the SessionStateMode enumeration. The following list describes the available session state modes:

  • InProc mode, which stores session state in memory on the Web server. This is the default.
  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • Custom mode, which enables you to specify a custom storage provider.
  • Off mode, which disables session state.

In Process session mode


In-process mode stores session state values and variables in memory on the local Web server. This is the simplest of all settings and will fail to work in a web garden or web farm scenario.
<sessionState mode="InProc" 
timeout="20"
cookieless="false">


State Server mode


StateServer mode stores session state in a process, referred to as the ASP.NET state service, that is separate from the ASP.NET worker process or IIS application pool. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.


To use StateServer mode, you must first be sure the ASP.NET state service is running on the server used for the session store. The ASP.NET state service is installed as a service when ASP.NET and the .NET Framework are installed. The ASP.Net state service is installed at the following location:
systemroot\Microsoft.NET\Framework\versionNumber\aspnet_state.exe



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>


Note that objects stored in session state must be serializable if the mode is set to StateServer or SQL Server (described below).

 

SQL Server mode


SQLServer mode stores session state in a SQL Server database. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.


To use SQLServer mode, you must first be sure the ASP.NET session state database is installed on SQL Server. You can install the ASP.NET session state database using the Aspnet_regsql.exe tool.


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


  • 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>

Storing user session is a simple yet powerful concept that you should know in depth before you go for a web developer interview. Even if you have not used all the different modes, it is imperative that you learn about them and play with them. Hopefully, this article has given you enough to start with.

10 comments:

  1. This is fabulous Man :) Thanks

    ReplyDelete
  2. Great work, really appreciate your effort for the information and being enrolled in http://www.wiziq.com/course/46-INTERVIEW-PREPARATION-COURSE-BY-SHIVPRASAD-KOIRALA the OOps, .NET and SQL INTERVIEW Preparation i found your information very helpful.

    ReplyDelete
  3. really good one

    ReplyDelete
  4. Thanks Yaar!!!!

    ReplyDelete
  5. Thank u ! really very Helpfull for the information

    ReplyDelete
  6. cool , Thanks a lot.

    ReplyDelete
  7. I liked it. How to use custom mode.

    ReplyDelete
  8. very nice information

    ReplyDelete