Out-of-Process Session State (In State Server) in ASP.NET

Session in ASP.Net is used to store state for the individual clients.

By default, sessions are stored in-proc (in-process); i.e. in the memory of the Web Server.

Then, why do we need Out-of-Process Session States??

  1. As we have mentioned above by default sessions are stored in-process; so when the process is recycled or if it fails (say, the server goes out of order) , the session state is lost, even if the browser has the Session Key stored with it.
  2. Also, an in-proc session doesn't go good with Web Farms.

Say if the application is deployed to the Web Farms with many machines and each request can be served by different machines, then an in-proc session state could not track the user.

To overcome the above issues, we can store a session Out-of-Process; in a local or remote NT Service.

  1. Open Visual Studio - create a new project - Web - ASP.Net Empty Web Application and let's name it "OutofProcSessionState"
  2. Add new Web Form Login.aspx as in the following:
    1. Add a new textbox to get the username and a button to Login:

      Login.aspx
      Enter User Name:

      <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
      <
      asp:Button ID="btnSubmit" runat="server" Text="Go to Home Page"    OnClick="btnSubmit_Click"
      />
       
    2. Login.aspx.cs: Add the handler code for the Submit button in the Code behind:
      • Store the User Name logged in by user in the session indexed by User Name.
      • Redirect the user to the Home Page on click of the button.

      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          Session["UserName"] = txtUserName.Text.ToString();
          Response.Redirect("Home.aspx");
      }
       

  3. Add new Web Form, Home.aspx:
    1. Home.aspx: Add a label control in the Home Page to show the logged in User Name.

      <label id="lblUserName" runat ="server"></label>
       
    2. Home.aspx.cs - In the Page Load event retrieve the User Name from the Session State.
      • Retrieve the User Name from the Session and display it.
      • If the User Name is not present in the session then show the user name as Anonymous User.

      protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["UserName"] != null && Session["UserName"] !="")
          {
              lblUserName.InnerText = Session["UserName"].ToString();
          }
       
          else
          {
              lblUserName.InnerText = "Anonymous User";
          }
                    

  4. Run the application with Login.aspx as the Start Page.
    1. Enter user name:

      NT-Services.gif
    2. Go to the Home Page.

      NT-Services1.gif
    3. So all fine, until here; the session has been, by default, stored in-proc and is working as expected.
    4. Now let us go and stop the web development server.

      NT-Services2.gif
    5. Next, go to Visual Studio and launch the application again and go directly to the URL:
      http://localhost:8130/Home.aspx (Port number may vary).

      NT-Services3.gif
    6. The User Name was stored in the session (in-proc) which was in memory of the web development server and since here we have restarted the server. The User Name was lost so "Anonymous User" is being displayed as the User Name based on the alternate logic we have mentioned in our code.
       
  5. Now let me demonstrate, to store the Session State (out-of-proc) in the State Server, in the local or remote NT Service.
  6. Open the Web.Config file and go to the Session State configuration file element, and specify the mode as:
    "State server"

    <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424">
    </sessionState>

    1. Mode="State Server" depicts that we are using an Out-of-Process State Server.
    2. We have also specified the State Server Connection String; so that the State Server can be found to retrieve the Session data.
    3. stateConnectionString="tcpip=127.0.0.1:42424" -> We need to provide the IP Address of the State Server along with the Port number; in this case we are running locally over the default Port #,so we have specified that as: tcpip=127.0.0.1:42424 (42424 is the default port for which the Service will listen).
  7. Since in this case we are running locally, let's start the Service in our machine:
    1. Go to Control Panel - Administrative Tools - Services

      NT-Services4.gif

      • If ASP.Net is installed in the server; we should see the ASP.NET State Service listed as shown above.
      • Right-click and start the service.

        NT-Services5.gif
      • Also in the actual scenarios, we would need to make this process to start automatically:

        NT-Services6.gif
     
  8. Now run the application. Enter the User Name in the Login Page and the same gets displayed in the Home Page as we saw before.
  9. Now let's stop the development server again, as we did in Step 4 and then go to Visual Studio and launch the application again; and go directly to the Home Page via the URL:
    http://localhost:8130/Home.aspx (note that the port # may vary) without closing the browser, to keep the client session identifier the same.

    NT-Services7.gif
  10. So here we were able to retrieve the User Name from the session, in spite of restarting the Web Server, since in this case our session was stored in a State Server.

So in this article, we have seen how to store a session Out-of-Process in a State Server.

I have attached the above demonstration code.

Happy Learning!

Up Next
    Ebook Download
    View all
    Learn
    View all