Arrao4u

…a blog by Rama Rao

Archive for the ‘StateManagement’ Category

Session Storage Providers

Posted by arrao4u on December 20, 2009

Main Contents

There are three types of storage providers for ASP.NET 2.0 as opposed to 1 provider in classic ASP Session object.
1. In Process Session State store (Stores sessions in ASP.NET memory cache–> Used in Classic ASP)
2. State Server Session State Store (Stores sessions in the ASP.NET State Server service).
3. Sql Session State Store (Stores sessions in Microsoft SQL Server Database and is configured with aspnet_regsql.exe).

To change the different provider of session state, you need to edit your web.config file.

<configuration>
<system.web>
<sessionState mode="Off|Inproc|StateServer|SQLServer|Custom"/>
</system.web>
</configuration>

InProcess Session State

InProc Session Model is the fastest,common and the default Session provider in ASP.NET 2.0. In proc Session State is what you normally use in Classic ASP. The Session key and data is stored inside the memory and therefore it is the fastest among all the session objects provider.  It has significant limitations on the reliability. If the worker process or application domain recycles, all the session state data is lost.
ASP.NET application may restart for number of reasons.

1. Changing and Editing web.config or Global.asax file.
2. Modified files in the \bin or App_Code directory.
3. The process model of the web.config or machine.config file indicating when the application should be
restart.
4. Application pool in IIS being recycled(either manually or scheduled).
5. Cannot be used on WebFarm

The conclusion, In Process Session State works great for smaller applications that require only single Web server (consists of single processor) and your website doesn’t store or rely heavily on session data.

Note:
If your web server consists of multiple processor and you have assigned your current worker process to be on multiple processor (You can set this option under your application pool Web garden settings), then you must not use In process Session State cause you will be losing Session state everytime the request is redirected to another processor.

Code Samples for Storing Data in Session objects
1. Storing String in Session Objects
Session["MyData"] = TextBox1.Text;

2. Storing Objects in Session Objects
Client oClient = new Client();
Session["MyData"] = oClient;

Code Samples for Retrieving Data in Session Objects
1. Retrieving String in Session Objects.
TextBox1.Text = Session["MyData"].ToString();

2. Retrieving Objects in Session Objects
Client oClient = (Client)Session["MyData"];

State Server Session State

Out of Process Session State is held in a process called aspnet_state.exe that runs as Windows Service. You need to start the service manually from the ServiceManager. This is because the state service is not started by default. To start the service manually, open the Service Manager and find Server called ASP.NET state Service.
or to run it manually from command line
net start aspnet_state
By default the State Service listens on port 42424 but this port can be changed at the registry key.
The Key is in
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_State\Parameters\Port

Edit your web.config files and change the InProc to State Server as the code shown below

<configuration>
<system.web>
<sessionState mode="StateServer"              stateConnectionString="tcpip=127.0.0.1:42424"/>
</system.web>
</configuration>

Note :
You could run the StateServer on your own local web server or on another machine separately. If you run the StateService on another machine, make sure that the port is open. You can check if the port is opened easily by running telnet command.
E.g telnet 192.168.6.1 42424
If you got the reply from telnet, it means the port is open.

Because your application’s code runs in the ASP.NET Worker Process and the StateService runs on separate Worker Process, object stored in the Session can’t be stored as references. Your objects must be marked by [Serializable] attribute. Only classes that marked with the [Serializable] attribute can be serialized.

Sample Code

C#

[Serializable]
public class Person {
public string firstName;public string lastName;public override string ToString() {
return firstName + " " + lastName;
}
}


VB.NET

<Serializable()>Public class PersonPublic firstName as String
Public lastName as String
Public Overrides Function ToString() As String 
Return (firstName + " "  + lastName)
End Function
End Class


After you have marked the class as serializable, then you can stored the objects in the Session variables
e.g
Person oPerson = new Person();
Session["MyData"] = oPerson;

For world class,highly available and scalable Website, consider using Session model other than in Proc. Even you can guarantee via your load balancing appliance that your session will be sticky , you still have applicatio recycling issues to contend with. The Out of Proc State Service data is persisted across application pool recycles but not computer reboots. However if your state service is stored on a different machine entirely, it will survive Web Server recycles and reboots.

SQL Session State

This is the last part of the Session State store that I am going to discuss. As you seen from above article, Session can be stored in memory and State Server. The last part would be storing the Session State in SQL Server.  InProc State Server offers speed, State Server offers resilience and speed balance while storing Sessions in SQL Server will offer reliability that can serve your sessions to a very  large web farm that can survive from IIS restarts or State Server restarts.

Before you can start using SQL to store your session state, you need to do few steps below to set up your database.

ASP.NET 2.0 provide a very handy and useful tools to help you configure the database. You need to use
aspnet_regsql.exe in your command prompt. The utility is located on C:\Windows\Microsoft.Net\Framework\<version>.
If you type aspnet_regsql in the command prompt, it will launch the GUI version of the aspnet tools, You cannot use it for configuring the database to store the Session.

Type the command line below to configure the database for Session support.
aspnet_regsql.exe -S localhost -U sa – P worldofasp -ssadd -sstype c -d dbName

Three options exists for -sstype (t,p or c)
The most significant differences is that the -ssytpe t option does not persist session state data across SQL Server restarts whereas the-sstype p option does.
In the example above,we use option -sstype c , so that you can specify custom database name to store your Session. If you use -sstype t or p then your Session database will be stored on default databasename called ASPState

Next step would be changing the connection string in web.config

<sessionState mode="SQLServer" sqlConnectionString="data source=127.0.0.1;
userid=sa;password=worldofasp;database=dbName"
/>

Storing Objects in SQL Store would be the same as storing objects in State Server. All the objects need to be marked as Serializable. You can check the Session values in your database after all the database has been created.

Conclusion

Before you decide which Session Store you are going to use in your websites, You need to analyze the requirements and how important session data to your website. If your site is running on very large web farm and you need extremely robust and reliable Session objects, then SQL Session Store will be the perfect one cause it can survive from IIS restarts, Server restarts or SQL server restarts.  But the bottleneck would be on the performance of your SQL Server machine. You need to have a very fast and reliable SQL Server cause every session data will be retrieved from your Database.

If you can live with litte bit of downtime of your session, then the perfect candidate for your Session store would be on State Server. Your session can only be disappear or resetted if your service has been restarted or your machine has been restarted. Please remember to set the Service Startup type to automatic or your website will not be able to use the State Service if your machine has been restarted and the service is not up. You might need to implement some scheduler to check State Server status and email you if there are some issue with the service.

Last and try to avoid would be using the InProc Session Store, I don’t really recommend this to store your Session Data. You will lose your Sessions quite easily especially if you are hosting your site in shared Hosting environment.  If your site is not storing important information in Session , then this will be the perfect choice since it is the fastest among all

ref:http://www.worldofasp.net/tut/Sessions/Understanding_Session_Object_in_ASPNET_20_80.aspx

Posted in ASPNET, Session Storage Providers | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.