ASP.NET Session State Modes: Some bullet points

Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Dive directly into topic, No beating around the bush

ASP.NET session state mode can be broadly classified into two modes: In-Proc and Out-Of-Proc.

As the name suggests In-Proc mode maintains session state data in memory of the web server. This means if the web application or the IIS restarts the session data is lost.

Out-Of-Proc maintains the data separately outside the memory of web server. This means your session data is not lost even if the web application/IIS restarts. This mode can again be classified into three categories: StateServer, SQLServer and Custom.


StateServer: Session state data is maintained by a service named ASP.NET State Server. To use this mode make sure the service is up and running on the server(the same can be checked using run-->services.msc). Also make change in the web.config to specify the mode as StateServer.
   1:   <sessionState mode="StateServer"></sessionState>



SQLServer: Session state is maintained in a SQL server database. In Web.config session state mode also needs to be changed. below is example. I’ve set Integrated Security to True for simplicity, one can surely SQL credential with proper permission.

   1:  <sessionState 
   2:           mode="SQLServer" 
   3:           sqlConnectionString="Data Source=MYCOMPUTER\SQLEXPRESS;Integrated Security=True">
   4:  </sessionState>

While using this mode ensure that ASPState database is installed on server.To add ASPState database follow the below mentioned steps:
  1. Go to <root>\WINDOWS\Microsoft.NET\Framework\<version> (for example: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319)
  2. Execute aspnet_regsql.exe and finish the steps.
  3. Go to CMD and navigate to the path said in step 1.
  4. Type the following command to finally add the ASPState database to your server: aspnet_regsql.exe -S MYCOMPUTER\SQLEXPRESS  -E –ssadd
-E denotes the currently logged on user
However –U and –P can also be used to specify SQL User ID and Password explicitly.
 Custom: Session state is maintained on custom storage(Frankly I did not google much on this).

One Major point to be noted: Objects to be stored in session in out-of-proc mode(StateServer & SQLServer mode for sure), must be serializable. Check the below code example:

   1:  [Serializable]
   2:  public class YourCustomClass
   3:      {
   4:          public string member1 { get; set; }
   5:          public string member2{ get; set; }
   6:   
   7:          public person(string m1, string m2)
   8:          {
   9:              member1 = m1;
  10:              member2= m2;
  11:          }
  12:      }



If you want to add objects of class “YourCustomClass” to the session, you must declare the same as Serializable by adding the attribute as shown above.
If the class is not specified as Serializable and code tries to add the objects of this class to session, code will throw HttpException exception. Check the below screen shot for better understanding:











Well, that all for today.
-Sayan
P.S: As always, don’t hesitate to correct me.

0 comments: (+add yours?)

Post a Comment