SQLServer Session Mode:-

  • This session mode provide us more secure and reliable session management in ASP.NET.
  • In this session mode, session data is serialized and stored in A SQL Server database.
  • The main disadvantage of this session storage method is the overhead related with data serialization and de-serialization.
  • It is the best option for using in web farms though.

To setup SQL Server, we need these SQL scripts:

  • For installing: InstallSqlState.sql
  • For uninstalling: UninstallSQLState.sql

The easiest way to configure SQL Server is using the_aspnet_regsql_command.

I have explained in detail the use of these files in the configuration section. This is the most useful state management in web farm scenarios.

When should we use SQLServer Session Mode

  • SQL Server session mode is a more reliable and secure session state management.
  • It keeps data in a centralized location (database).
  • We should use the SQLServer session mode when we need to implement session with more security.
  • If there happens to be frequent server restarts, this is an ideal choice.
  • This is the perfect mode for web farm and web garden scenarios (I have explained this in detail later).
  • We can use SQLServer session mode when we need to share session between two different applications.

Advantages:

  • Session data not affected if we restart IIS.
  • The most reliable and secure session management.
  • It keeps data located centrally, is easily accessible from other applications.
  • Very useful in web farms and web garden scenarios.

Disadvantages:

  • Processing is very slow in nature.
  • Object serialization and de-serialization creates overhead for the application.
  • As the session data is handled in a different server, we have to take care of SQL Server. It should be always up and running.

Configuration for SQLServer Session Mode

In SQLServer session mode, we store session data in SQL Server, so we need to first provide the database connection string in web.config. ThesqlConnectionStringattribute is used for this.

After we setup the connection string, we need to configure the SQL Server. I will now explain how to configure your your SQL Server using the_aspnet_regsql_command.

Step 1: From command prompt, go to your Framework version directory. E.g.:c:\windows\microsoft.net\framework\<version>.

Step 2: Run the_aspnet_regsql_command with the following parameters:

Have a look at the parameters and their uses:

Parameters Description
-ssadd Add support for SQLServer mode session state.
-sstype p P stands for Persisted. It persists the session data on the server.
-S Server name.
-U User name.
-P Password.

After you run the command, you will get the following message:

That's all.

Step 3: Open SQL Server Management Studio, check if a new database ASPState has been created, and there should be two tables:

  • ASPStateTempApplications
  • ASPStateTempSessions

Change the configuration string of the StateServer example and run the same sample application.

Just store the roll number and user name and click on the Submit button. Open the_ASPStateTempSessions_table from SQL Server Management Studio.. here is your session data:

Now do the following test that I have already explained in the StateServer mode section:

  1. Remove the Serializekeyword from the StydentInfoclass Reset IIS and click on Restore Session
  2. Stop SQL Server ServicesI think I have explained the SQLServer session mode well.

results matching ""

    No results matching ""