Session State Management
Session State Modes Available in ASP.NET
- InProc
- Sessions are stored in worker process memory.
- Performance wise it is fastest but performance could be decreased if large and persistent objects are stored for a long time.
- Session data will be lost if worker process or the app domain recycles. Following could the reasons if session data is getting lost in InProc mode.
i. Global.asax, web.config or machine.config is changed.
ii. Bin directory of the application is modified.
iii. When virus scanning software touches some .config file or probes into bin directory.
iv. Various values of memoryLimit attribute inside <processModel> section.
- This mode will not work in web garden mode or in the NLB environment.
- Session_End event is only fired when InProc mode is used.
- State Server
- Sessions are stored in memory outside of aspnet worker process.
- State server where sessions are stored can be run on another server.
- Communication between aspnet worker process and the sate server is done in TCP.
- It is slower then InProc mode since communication overhead is present between worker process and state server.
- Serialization and De- Serialization of data also takes time and can affect performance.
- Basic/Predefined data types are serialized/de-serialized by asp.net but other data types used binary formatter for doing this and it hits performance.
- Can be used if web gardens are configured or if the application is running on NLB environment.
- We have to make sure that the objects are serializable.
- If Web farm is there make sure <machineKey> on each web server is same. Please follow the link to generate the machine key from http://support.microsoft.com/?id=313091 . (NOTE: - Machine key is also needed when we use: -
i. Forms Authentication in web farm scenario.
ii. When we want view state to be available across the web farm.
- If session state is still not persisting in the network load balanced scenario make sure that Application path for the website on each server is same. Instance ID’s of application on each server should be the same.
- SQL Server
- Sessions are stored in SQL server.
- Sessions inside SQL server can be persistent or non-persistent. It depends on the SQL script that we ran from C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322. If worker process recycles in both of the cases session data will be restored. No matter what type of SQL Script we ran. But if we want to persist session data even after the reboot of SQL servers then we have to run InstallPersistSqlState.sql rather than InstallSqlState.sql.
- It is even slower then State Server mode. So while designing the application we need to take care of the performance.
- All the web farm and NLB settings that apply on State Server are similar to SQL Server.
- In framework 1.0 if we try to store objects in session which are not serialized then we may encounter hangs in the application. However this problem is fixed in V 1.1.
Some Troubleshooting tips on session problems
- Sessions may not work for some clients (browsers) if cookies are turned off. Here is the sample java script code in order check whether the cookies are enabled.
<html>
<head>
<script language="javascript">
function CheckCookies()
{
//Create a Test cookie.
var testcookie = 'jscookietest=valid';
// Add this cookie in the cookie in the cookies collection of the document object.
document.cookie = testcookie;
if (document.cookie.indexOf(testcookie) == -1)
{
// If cookies is not created it means that cookies are not supported. So just redirect user to HTML/ASPX Page
// stating that cookies are enabled.
window.location.href = "Any HTML Page";
return false;
}
// It means cookies are enabled.
return true;
}
</script>
</head>
<body onLoad="javascript: return CheckCookies();">
Cookies are enabled.
</html>
- Sessions may not work if SessionStateModule is not registered inside machine.config file. Normally we can find its entry in machine.config as
<httpModule>
<add name="Session"
type="System.Web.SessionState.SessionStateModule"/>
………………
</httpModule>
Also note that sometime share point removes this module inside its web.config and as a result session state is not available.
NOTE: - SessinStateModule helps in managing session states. Its purpose is to generate or obtain session ID and for storing and retrieving session data for every request.
That's all for now on Session Mgmt. I will keep adding more and more findings as an when i encounter them in ASP.NET 1.0/1.1 as well as 2.0 !!!