ASP.NET Tutorial – Easy Steps to Move from Session InProc Mode to Session StateServer
What is In-Process Mode in ASP.NET?
What is State Server Mode in ASP.NET?
StateServer mode stores session state in a process, referred to as the ASP.NET state service, that is separate from the ASP.NET worker process or IIS application pool. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.To use StateServer mode, you must first be sure the ASP.NET state service is running on the server used for the session store. The ASP.NET state service is installed as a service when ASP.NET and the .NET Framework are installed. The ASP.Net state service is installed at the following location:
systemroot\Microsoft.NET\Framework\versionNumber\aspnet_state.exe
To configure an ASP.NET application to use StateServer mode, in the application’s Web.config file do the following:
- Set the mode attribute of the sessionState element to StateServer.
- Set the stateConnectionString attribute to tcpip=serverName:42424.
When You Should Move from Session InProc Mode to Session StateServer?
Main Advantage of Session StateServer (Best to choose while hosting on third party server)
- Session is persistent and reliable.
- Avoid Session Timeout due to Memory shortage on server (IIS Setting).
Main Disadvantage
- Poor Performance compare to Session=”InProc”
- Session_End Event would not fire.
Steps for Moving Session InProc Mode to Session StateServer Mode
Step 1: Start Asp.net State Service
- Go to Control Panel > Administrative Tools > Services
- Select Asp.Net State Service.
- Right Click on Asp.net State Service and choose start from popup menu.
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.
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
Understanding Serialization in C#
[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; } } }
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.
- Mark SQLConnection Object as NonSerialized
[NonSerialized] protected SqlConnection _mainConnection; - 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(); } }
No #1 Recommended ASP.NET Hosting ASPHostPortal.com
ASPHostPortal.com is the leading provider of Windows hosting and affordable ASP.NET Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.