Monday, January 27, 2014

ASP.NET Session modes

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>

No comments:

Post a Comment