How to enable SQL server based session state for ASP.Net

by Viper 13. May 2009 08:03

For high performance web applications caching of data plays a significant part in improving performance. We all use session or application cache to store data in one form or the other. By default ASP.Net is configured to use InProc session storage mechanism. What this means is that all session data is stored in-memory on web server that is serving ASP.Net application. But when you are developing high availability web applications, you will find your self hosting your web application across web farms or web gardens in network load balanced systems. What this means is that user requests are served from multiple web servers. And load-balancer will pick the web server based on some internal load balancing algorithm. Unless you use stick IPs, you don't have control on what web server will be picked for next request. So if you are using InProc mechanism for session state management, you have a problem. Your session state is saved on one web server where as next request may be served from different server. So you end up loosing session state for that request. This is where you will need to use an external storage for storing session state that can be accessed across multiple servers.

ASP.Net framework provides mechanism to use SQLServer as an option that you can set in sessionState settings in your web application's configuration. Before you can start using sql server to store and server session state, you will need to set up some tables and means to store this data in SQL server. When you install ASP.Net framework on your server, Microsoft provides some SQL scripts along with an installer application that helps you in preparing your SQL Server for session state management.

If you look in WINDOWS\Microsoft.NET\Framework\{Version}, you will find an executable with name aspnet_regsql.exe. This is the same executable that you may have used to install membership and personalization database. If you supply specific command line parameters to this executable it will install required tables and database to manage session state. A very simple command to install session related database and tables can look like as below

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regsql.exe -ssadd -sstype p -E
Start adding session state.
....
Finished.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>

You will find a new database named ASPState in your SQL server now. You can have more control on creation of this database by using appropriate command line options. Following is list of options that you can use.

-- SESSION STATE OPTIONS --

-ssadd                  Add support for SQLServer mode session state.

-ssremove               Remove support for SQLServer mode session state.

-sstype t|p|c           Type of session state support:

                        t: temporary. Session state data is stored in the "tempdb"
                        database. Stored procedures for managing session are installed in
                        the "ASPState" database. Data is not persisted if you restart
                        SQL. (Default)

                        p: persisted. Both session state data and the stored procedures
                        are stored in the "ASPState" database.

                        c: custom. Both session state data and the stored procedures are
                        stored in a custom database. The database name must be specified.

-d <database>     The name of the custom database to use if -sstype is "c".

Once you have created the database for session state management, modify web.config file of your application to start using SQLServer mode for session state management.

<sessionState 
    mode="SQLServer"
    sqlConnectionString="data source=127.0.0.1;
        user id=<username>;password=<strongpassword>"
    cookieless="false" 
    timeout="20" 
    />

Important step

After you uninstall SQL Server mode session state management configuration, you must restart the w3svc service. To restart the w3svc process, type net start w3svc at a command prompt.

Views: 352

Tags:

ASP.Net

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.6.1
Theme by Naveen Kohli

Recent

By Categories