Tuesday, May 13, 2008

Session Managment in Asp.net

Session management in asp.net means how and where session information is saved.
There are three types of session management in Asp.net

1-In process
2-SqlServer
3- State Server


By default all the applications are configured to in process it means that session information is being saved in the same process that application is running. So far so good you do not need change this however you can apply some session configuration like sessiontimeout which is an indicator for asp.net to know howlong should keep a session for user. For example if a user does not send any request to server for longer time than sessiontimeout (which is minutes) then asp.net will kill that session. Or you can make it cookiless it means that it will use query string to notify each session. All of these configuration can be done in web.config with sessionstate tag :

sessionState mode="InProc" cookieless="false" timeout="20"

You do not need to change the mode as long as you have just one server. If you have more than one server to serve your application then you need to use other session management (2 or 3). When your application is big enough and you have a lot of users you need to deploy your application in different servers (it is called webFarme). If you use InProcess approach, you cannot trust on session information the reason is that InProcess means each server has its own session information so imagine a user come to your site the first request goes to one server and second request of the same user will go another server so your session id will be different and second server has no idea about session information in first server. To solve this issue we need save session information in somewhere else rather than in each server. (I am sure everybody knows that to have multiple servers for one application we need load balancing service be installed between those servers).


StateServer:


Saving session information in one server and let other servers use that information from that server to do this you need ASP.NET State Service to be up and running (this service is installed when you install the .NET runtime). By default the service listens over port 42424. To see this service you can go to control panel\ Administrative tools \ services you should be able to find ASP.NET State Service. Using the State Service option is useful when you want out-of-process session state management but do not want to have to install SQL Server on the machine hosting the state. See this sample:

sessionState mode="StateServer" stateConnectionString="192.168.1.103:42424"

SqlServer :

Before using this mode, you must run the InstallSqlState.sql script on the database server where session state will be stored. This script can be found in :


C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallSqlState.sql


(if you have another version you can try those ones)
The primary purpose of this script is to create some tables that can store client-specific state indexed by session ID in those tables. After running that script in your database (you do not need specify any specific database just run the query in query analyser in sqlserver)
See this sample:

sessionState mode="SQLServer" sqlConnectionString= "data source=192.168.1.103;user id=sa;password="

Microsoft suggest the sql server approach.

How to Find an object in a generic List (Delegate sample)

In this lesson I am going to show you more examples about delegate. One of the best example of delegate could be Find method in generic list.
See this code:

public List personlist = new List();
private string searchName;
public void FillList()
{
personlist.Add(new Person("Alex", "Botco", 5));
personlist.Add(new Person("Tai", "Chang", 6));
personlist.Add(new Person("Ehsan", "Parsa", 9));
}
public Person FindPersonbyName(string name)
{
searchName = name;
return personlist.FindLast(new Predicate(this.CheckName));
}
private bool CheckName(Person p)
{
return p.Name == searchName;
}
public Person FindPersonById(int Id)
{
return personlist.FindLast(delegate(Person p) { return p.ID == Id; });
}

In this example as you see first in FillList Method some persons are added to personlist each person has name, family and Id. Now we want to search a person based on Id or name. VS 2005 for its generic list has some methods like Find, FindLast, ... these methods accept a delegate (an address of a method). The delegate definition is implemented by Microsoft which is like this:

public delegate bool Predict (T item);

in our example T is Person. Microsoft is using this delegate (which is a method you will write and assign to this delegate) to Find an Item in list. Since you are writing the logic of this method so you can compare whatever you need in the method assigned to delegate.

If you look at the FindPersonbyName method I created an object of delegate and then I addressed the checkName method. And in CheckName I compared names. The downside of this type of coding is that I had to keep name in another variable (searchName) then in check name I compared the person name with searchName.
Another way to write the code is using delegate keyword inside FindLast method you can see an example of this in FindPersonById in this method instead of creating method and then create an object of delegate an assign that method to delegate we are writing the whole implementation in one line.
The syntax always is like this first delegate keyword then imagine we are writing a method so next step is having a parameter again this parameter is always an instance of object that we have in list. In our example that object is Person. Then in {} we write the code as if we were writing inside a method again always we are using the instance we declared in parameter and then compare one of its properties with other variables. The good point about writing code like this is that we do not need to define extra field like searchName in last methd.

I am going to dedicate this article to my dear friend Jeff.