Introduction

Session in ASP.Net is used to store the state for the individual clients. By default, sessions are stored in-proc (in-process), i.e., the memory of the Web Server.

Why do we need Out-of-Process Session States?

  1. As mentioned above, by default, sessions are stored in-process, so when the process is recycled or 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 well with Web Farms.

If the application is deployed to the Web Farms with many machines and each request can be served by different devices, 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.

Step 1. Open Visual Studio - create a new project - Web - ASP.Net Empty Web Application and let's name it "OutofProcSessionState."

Step 2. Add new Web Form Login. aspx as in the following:

Step 3. Add new Web Form, Home. aspx:

Step 4. Run the application with Login. aspx as the Start Page.

Step 5. Now let me demonstrate to store the Session State (out-of-proc) in the State Server, in the local or remote NT Service.

Step 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" indicates that we use 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).
  4. Since, in this case, we are running locally, let's start the Service on our machine:
    1. Go to Control Panel - Administrative Tools - Services

      NT-Services4.gif

      • If ASP.Net is installed on the server, we should see the ASP.NET State Service listed above.
      • Right-click and start the Service.

        NT-Services5.gif

      • Also, in the actual scenarios, we would need to make this process start automatically:

        NT-Services6.gif

  5. Now run the application. Please enter the User Name in the Login Page, displayed on the Home Page, as we saw before.
  6. Now let's stop the development server again, as we did in Step 4, and then go to Visual Studio and relaunch the application, 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

  7. So here we retrieved the User Name from the session, despite restarting the Web Server since, in this case, our session was stored in a State Server.

Conclusion

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!