Recently I was working on a application that was required to be hosted on multiple web servers. Sounds simple and many of us has already done so may be more than once. One of the key issues that arise is maintaining session states across multiple web servers so that the user request can be routed to any web server. Asp .Net provides very elegant solutions out of the box. We can use Sql Server or an additional State Server. The mechanism used is called OutOfProcess Session Management. Whenever out-of-process session state is specified, it is also important to realize that anything placed into session state is serialized and passed out of the ASP.NET worker process. Thus, any type that is stored in session state must be serializable for this to work properly.
public class YourClassName
// class implementation
StateServer mode 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.
The .Net Framework Installs a service called ASP .Net State Service which handles the sessions when Out of Process.
In order to set up SessionState server modify the web.config
<sessionState mode=”StateServer”stateConnectionString=”192.168.1.103:42424″ />
42424 : is the default port. It can be updated by setting the registry entry : HKLM\System\CurrentControlSet\Services\aspnet_state\Parameters\Port
To configure SQLServer Mode, you must run InstallSQLState.sql on the database. The primary purpose of this script is to create a table that can store client-specific state indexed by session ID in the tempdb of that SQL Server installation. The ASP state table is created in the tempdb database, which is not a fully logged database, thus increasing the speed of access to the data. In addition to storing the state indexed by the session ID, this table keeps track of expiration times and provides a locking mechanism for exclusive acquisition of session state. The installation script also adds a job to clean out all expired session state daily.
<sessionState mode=”SQLServer” sqlConnectionString= “data source=192.168.1.103;user id=sa;password=” />
Note: no database is specified in the connection string because it is assumed tobe TempDB.
Both the state server and the SQL Server session state options store the state as a byte stream—in internal data structures in memory for the state server, and in a VARBINARY field (or an IMAGE field if larger than 7KB) for SQL Server.