articles

Home / DeveloperSection / Articles / StateServer session state in asp.net

StateServer session state in asp.net

StateServer session state in asp.net

Anchal Kesharwani10501 24-Jun-2014

In this article describe the concept of the StateServer session state mode in asp.net. Here we understand the StateServer session state mode and learn from the example.

In this article describe the concept of the SQLServer session state mode in asp.net. Here we understand the SQLServer session state mode and learn from the example.

ASP.NET supports various Session State Modes depending on various storage options for session data. The following are the available Session State Modes in ASP.NET:

·         InProc

·         StateServer

·         SQLServer

·         Custom

Let’s we talk about StateServer session or StateServer mode session in asp.net:

It is also called out-proc session mode. 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.

This server may run on the same system, but it's outside of the main application domain where your web application is running. This means if you restart your ASP.NET process, your session data will still be alive.

Advantages of StateServer Session mode:

•Big improvement of State Server, when compared to InProc mode, is that sessions' data survive if ASP.NET application restarts. State Server use different working process, so if ASP.NET process restarts, that doesn't affect State Server and session variables remain intact.

•If dedicated State Server is used, session data are stored on other computer. In this case web server's memory remains free, so web application executes faster.

•State Server supports web gardens and web farms.

Disadvantages of StateServer mode

•State Server requires additional configuration, and in case of dedicated State Server, one more machine to obtain and maintain.

•State Server is usually not allowed by shared web hosting providers. If you use shared web hosting, you probably have not access to State Server.

•If State Server is restarted, session data are lost.

•State Server is slower than InProc mode.

•Process is slow due to serialization and de-serialization.

Here the following example we understand:

1.       Create a user form (User.aspx):

<form id="form1" runat="server">

    <div>

    Name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>

    <br /> <br />

        <asp:Button ID="btn_submit" runat="server" Text="Submit"OnClick="btn_submit_Click" />

    </div>

    </form>

 

2.    We write the code User.aspx.cs on button submit click event

 

protected void btn_submit_Click(object sender, EventArgs e)

    {

        Session["Username"] = txt_name.Text;

        Response.Redirect("UserDetails.aspx");

    }

 

3.     Add a web from UserDetail.aspx which contain the code in User.aspx.cs on page load:

protected void Page_Load(object sender, EventArgs e)

    {

        if (Session["Username"] != null && Session["Username"] != "")

        {

            string name = Session["Username"].ToString();

            Response.Write(String.Format("User name is {0}", name));

        }

        else

        {

            Response.Write(String.Format("There are no user!"));

        }

    }

 

4.       Run the application with User.aspx startup page.

1.       Enter User name

StateServer session state in asp.net

2.       Click on Submit button to submit the page.

StateServer session state in asp.net

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.


5.       Next, go to Visual Studio and launch the application again and go directly to

the URL: http://localhost:62184/UserDetails.aspx (Port number may vary).


StateServer session state in asp.net

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 "There are no 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"

configuration>

  <system.web>

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

  </system.web>

</configuration>

               

1.       Set session state Mode="StateServer"

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.       So, we start the service in this case we are running locally, let's start the Service in our machine:

1.       Go to Control Panel - Administrative Tools – Services

StateServer session state in asp.net

2.       Right click on ASP.NET State Service and click on properties option to set

properties of  ASP.NET State Service


StateServer session state in asp.net 


3.       Select startup type ‘Automatically’ and start the ASP.NET State Service.

 

StateServer session state in asp.net 


8.       Now again run the application and enter the user name and press submit

button to go to the UserDetail.aspx.


9.       Stop the development server again and direct launch the UserDetail.aspx

from visual studio and go to the directly on URL:

http://localhost:62184/UserDetails.aspx (Port number may vary).


StateServer session state in asp.net

10.   Instead of restarting the Web Server we get the Username from the session,

since in this case our session was stored in a State Server.


Updated 10-Feb-2020

Leave Comment

Comments

Liked By