In the world of web applications Sessions are commonly used to keep key information about users. Before going into the details of State Server lets have a quick look over different session management solutions provided by ASP.Net. Here are the different modes for storing session in ASP.net:-
This is the default mode. In this session data is stored in web server's (IIS) memory space. In case IIS is restarted all sessions data will be loss when this mode is configured.
This is the out of the process way of storing session. In this mode session data is stored in a separate process (ASP.Net State Service). Incase IIS is restated then session data will not be effected when this mode is configured.
In this mode session's data is stored in SQL server. Incase IIS or SQL Server is restarted session will not loss. But is slower than State Server.
In this mode we can specify any custom provider.
In this mode sessions are disabled.
Now if your application is hosted on a web farm/garden then users will claim that their sessions is getting loss too frequently. Consider a scenario when there are two servers hosting an application. Now if one request goes to server 'A', its session will be created on that particular server and in case other request from same user goes to server 'B' then on that server session will not exist so user have to log in again. Also if you are using Inproc mode and your IIS is restarted due to recycling or application deployment then all of your session will lost. In such scenarios we can configure seperate state server to avoid such problems.
How to configure State Server in ASP.Net
There are three steps to configure state server:-
- State Server Service Configuration
- Session State Section Configuration
- Machine Key Configuration
1- State Server Service Configuration
First go to Windows Control Panel -> Administrative Tools and Open Services. Now search for 'ASP.NET State Service'. Now set service start-up type to Automatic. Generally we configure a separate server as a state server but by default it is disabled. To enable saving state for remote machines, we have to configure some registry settings. So open registry editor and navigate to following location:-
Now you have to set two parameters here:-
AllowRemoteConnections: It will allow other computers to use this computer as state server. Set it to 1. Default value is 0.
Port: Specify the port on which state service will run. By Default it is 42424. Keep it same unless you have some specific reason.
2- Session State Section Configuration
After that you have to modify your application's web.config file and set Here is a sample:-
<?xml version="1.0"?> <configuration> <system.web> <sessionState mode="StateServer" stateConnectionString="tcpip=Type_State_Server_IP_Here:42424" cookieless="false" timeout="20" /> </system.web> </configuration>
3- Machine Key Configuration
Now we are almost done with state server configuration. One last thing is to set machineKey in our application's web.config. Here is a sample web.config file with machine key.
<?xml version="1.0"?> <configuration> <system.web> <machineKey validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B" decryptionKey="ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F" validation="SHA1" decryption="AES"/> </system.web> </configuration>
You can generate a machine key from Pete's Nifty Machine Key Generator
Advantage and Disadvantage of State Server
Here are few things which you should keep in mind before going for state server:-
- Since session data is stored in a separate location so any issue with IIS will not affect sessions.
- User will not face session loss in case of web farm or web garden.
- This approach is slower than InProc because of object serialization/de-serialization.
- State server must be running otherwise application will not work.
- In InProc mode any object can be stored in session but in state server case objects should be serializable.
State Server is a must have when you are using session and working web farm or web garden. Feel free to post comments.