Tuesday, 22 July 2014

What are the different modes available in session in asp.net.


ASP.NET Session modes

 

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
We can choose the session state provider based on which session state we are selecting. When ASP.NET requests for information based on the session ID, the session state and its corresponding provider are responsible for sending the proper information. The following table shows the session mode along with the provider name:


Session State Mode
State Provider
InProc In-memory object
StateServer Aspnet_state.exe
SQLServer Database
Custom Custom provider


Apart from that, there is another mode Off. If we select this option, the session will be disabled for the application. But our objective is to use session, so we will look into the above four session state modes.
     The answer is quite simple if you have ever added the <sessionState> element in web.config:


    
     ASP.NET session state supports several different storage options for session data. Each option is identified by a value in the SessionStateMode enumeration.

In Process session mode:

      It stores session state in memory on the Web server. This is the default.

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:


     It stores session state in a separate process called the ASP.NET state service.
     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

<configuration>
    <system.web>
        <sessionState mode="StateServer"                      stateConnectionString="tcpip=SampleStateServer:42424"
                      cookieless="false"
                      timeout="20"/>
    </system.web>
</configuration>

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.

Custom mode:

It enables you to specify a custom storage provider.
Off mode:

It disables session state.

No comments:

Post a Comment