Types of Session State in ASP.NET
Session State Mode | State Provider
InProc | In-Memory Object
StateServer | Aspnet_state.exe
SQLServer | DataBase
Custom | Custom Provider
InProc:
When the session state mode is set to InProc, the session state variable are stored on the web server memory inside the asp.net worker process. This is the default session state mode.
Advantages:
- Very easy to implement. All that is required is, to set, the session state mode-InProc in web.config file.
- Will perform best because the session state memory is kept on the web server, within the ASP.NET worker processes (w3wp.exe)
- Suitable for web application hosted on a single server.
- Objects can be added without serialization.
Disadvantages:
- Session state data is lost, when the worker process or applicationpool is recycled.
- Not suitable for webforms and webgardens.
- Scalability could be an issue.
StateServer Mode:
When the session state mode is set to StateServer, the session state variables are stored in a process, called as asp.net state service. This process is different from the asp.net worker process. The asp.net state service can be present on a web server or a dedicated machine.
Advantages:
- ASP.NET worker process independent. Survives worker process restart.
- Can be used with web farms and web gardens.Suitable for web application hosted on a single server.
- State server offers more scalability than InProc.
Disadvantages:
- StateServer is slower than InProc.
- Complex objects, need to be serialized and deserialized.
- If the StateServer, is on a dedicated machine, and if the server goes down all the sessions are lost.
SQLServer:
When the session state mode is set to SQLServer, the session state variables are stored in a SQLServer database.
Advantages:
- SQLServer is the most reliable option. Survives worker process recycling and SQL Server restarts.
- Can be used with web farms and web gardens.
- More scalable than State server and InProc session state modes.Objects can be added without serialization.
Disadvantages:
- Slower than StateServer and InProc session state modes.
- Complex object, need to be serialized and deserialized.
Note:
Web Garden: Web application deployed on a server with multiple processes.
Web Farm: Web application deployed on muitiple server.