Many articles are discussing about advantages of using Session StateServer or SQLServer over InProc Mode. One basic reason why I choose StateServer Mode is when your website is running on Third Party Hosting than you will notice that Session Timeout can occur anytime depends on load of traffic on your server.
If your website has large number of visitors and session timeout can cause problem, It is better to change Session Mode Session="InProc" to Session="StateServer".
Main Advantage of Session StateServer (Best to choose while hosting on third party server)
1. Session is persistent and reliable.
2. Avoid Session Timeout due to Memory shortage on server (IIS Setting).
Main Disadvantage
1. Poor Performance compare to Session="InProc"
2. Session_End Event would not fire.
Now lets understand
Steps for changing Session InProc Mode to Session StateServer Mode.
Step 1: Start Asp.net State Servcie
1. Go to Control Panel > Administrative Tools > Services
2. Select Asp.Net State Service.
3. Right Click on Asp.net State Service and choose start from popup menu.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinBKb0f73tWnsVJmMOEfc0_U6LRD_mnzIvulSTC2u6VSwvQxFhqj2pKzde60b18qBv_Ytpfu5qoIVRCrIEBbwOIgmj2CfODtoUP6Bcdzv_QXLCSG46jCEwIp2-D-JN85zdxCvUmg/s400/service1.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiywZS2g6vBRPwjJBOL_yCohr2A3sv_zS5-LuCP5XhrYuvPMkfzDyHtGgbyzU-Vt6lS9In1Kq11AvlHBfZQ-a5UUcSCaOoQbKM9YL55fhYK1zztgY5fcSCWWETYU0cLqUBC5aESpg/s400/Start1.jpg)
If you forget to Start Service you will receive following error.
Server Error in '/' Application.
Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection. If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Web.HttpException: Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection. If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name.
Step 2: Change Session Mode in Web.Config File
<sessionState mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424"
cookieless="false"
timeout="120"/>
Note: You can adjust timeout minutes based on your requirement. Let the tcpip server to be localhost i.e. 127.0.0.1. Most webhosting company uses these settings unless you have different IP for StateServer and You are not required to change Port Number.
Step 3: Make All Object Serializable
You should make all object in your website to serializable.
Note: To take advantage of Session StateServer or SQLServer or Custom Mode, object needs to be serialized.
Example: If your project contains class files for creating DAL (Data Access Layer), you should append Serializable to every class definition in order to make class Serializable.
[Serializable]
Class Department
{
long _deptId;
string _deptName;
public long DeptId
{
get { return _deptId; }
set { _deptId = value; }
}
public string DeptName
{
get { return _deptName; }
set { _deptName = value; }
}
}
IMPORTANT While doing Serialization
Remember SQLConnection cannot be serialized.
You might receive following error if you don't handle this situation.
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Web.HttpException: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
So to handle this situation you need to do following thing.
1. Mark SQLConnection Object as NonSerialized
[NonSerialized] protected SqlConnection _mainConnection;
2. Mark your Class as Serializable and derive from IDeserializationCallback
Example:
[Serializable]
Class Department: IDeserializationCallback
{
long _deptId;
string _deptName;
public long DeptId
{
get { return _deptId; }
set { _deptId = value; }
}
public string DeptName
{
get { return _deptName; }
set { _deptName = value; }
}
//Create this Method Inside your Class
void IDeserializationCallback.OnDeserialization(object sender)
{
//Recreate your connection here
_mainConnection = new SqlConnection();
_mainConnection.ConnectionString =
ConfigurationSettings.AppSettings["connStr"].ToString();
}
}
11 comments:
nice post! THanks!
Hi, excelent post!,
I have a problem related it.
I am receiving always the same message, when i try to login:
Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection. If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name.
can you help me pls?.
Best Regards,
Cristian.-
You should always use 127.0.0.1, because whenever you are publishing your application on webserver it becomes local to that machine.
Hi, i am using the local ip
sessionState mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false"
timeout="20"
i tried with tcpip=127.0.0.1:42424 and tcpip=localhost:42424 , and i have the same message.
any other idea?
tks for your answer
Regards
Have you check that you have started service correctly.
1. Go to Control Panel > Administrative Tools > Services
2. Select Asp.Net State Service.
3. Right Click on Asp.net State Service and choose start from popup menu.
Hi Again, the Asp.net State Service is running.
I'm sorry for not put that before,
i restart the iis and i registered asp too. and nothing change.
any other idea?
tks for your help
1. web.config:
2.Administrator Tools > Services > ASP .Net State Service (Should Run)
3 In Registry Localmachine >SYSTEM >CurrentControlSet >Services >Aspnet_State >Paramaters > AllowRemoteconnection -0 and Port - 42424
4.Publish Your application with IIS server and browse your application
i got same kind of problem and i have resolved using above configuration...
Happy coding...
am not using DAL its not a n-tier apps will you please tell me how do i proceed to use state server mode
please send me the process without using DAL .we are not using DAL ...
Hi,
I think, the important thing in this topic is make serialize on the object class.
I applied your topic to my project, it works fine.
Thank you,
Sakon R.
Thank you,
Post a Comment