articles

Home / DeveloperSection / Articles / In-proc session state in asp.net

In-proc session state in asp.net

Anchal Kesharwani6213 24-Jun-2014

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

Web is stateless, which means a new instance of a web page class is re-created each time the page is posted to the server. As we all know, HTTP is a stateless protocol, it can't hold client information on a page. If the user inserts some information and move to the next page, that data will be lost and the user would not be able to retrieve that information. What do we need here? We need to store information. Session provides a facility to store information on server memory. It can support any type of object to store along with our own custom objects. For every client, session data is stored separately, which means session data is stored on a per client basis.

ASP and ASP.NET applications maintain a user’s session using a feature called session state.

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 In-proc session or In-process mode session in asp.net:

The In-proc session state mode is the default session state mode. This is the best session mode for web application performance. But the main disadvantage is that, it will lose data if we restart the server. In-proc session mode stores its session data in a memory object on the application domain. This is handled by a worker process in the application pool. So if we restart the server, we will lose the session data. If the client request for data, the state provider read the data from an in-memory object and returns it to the client. In web.config, we have to mention the session mode and also set the timeout.

ASP.NET provides two events that help you manage user sessions. These events are defined in the Global.asax file of the web application.

·         Session_Start() : This event occurs when a new session begins.

·         Session_End() : This event occurs when a session is abandoned or expires.

In in-proc session state mode is only supports the Session_End() event. The general flow for the InProc session state is like this:

In-proc session state in asp.net

In the in-proc session state mode is the default session state mode but you can also set the session state mode and session timeout in the web.config file in your application in following code:

<configuration>
  <system.web>    
    <sessionState mode="InProc" timeout="25"></sessionState>
  </system.web>
</configuration> 

If you don’t define timeout it is by default 20 minutes.

Here we give the example to understand in-proc session state mode.

Example
Step 1


Here we the create user form (User.aspx):

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

    <div>

        <table style="width: 34%;">

            <tr>

                <th>First Name</th>

                <th>:</th>

                <td><asp:TextBox ID="txt_firstname" runat="server" Width="144px"></asp:TextBox></td>

            </tr>

            <tr>

                <th>Last Name</th>

                <th>:</th>

                <td><asp:TextBox ID="txt_lastname" runat="server" Width="144px"></asp:TextBox></td>

            </tr>

            <tr>

                <th class="auto-style1">E-mail</th>

                <th>:</th>

                <td class="auto-style2"><asp:TextBox ID="txt_email" runat="server"Width="144px"></asp:TextBox></td>

            </tr>

            <tr>

                <td></td>

                <td></td>

                <td><asp:Button ID="btn_Submit" runat="server" Text="Submit"OnClick="btn_Submit_Click" /></td>

            </tr>

        </table>

    </div>

    </form>

 
Step 2


Write code behind on submit button:

protected void btn_Submit_Click(object sender, EventArgs e)

    {

        Session["FirstName"] = txt_firstname.Text;

        Session["LastName"] = txt_lastname.Text;

        Session["E-mail"] = txt_email.Text;

        Response.Redirect("UserDetail.aspx");

    }

Step 3


Code into the UserDetail.aspx on page load:

 

protected void Page_Load(object sender, EventArgs e)

    {

        try

        {

            string firstname = Session["FirstName"].ToString();

            string lastname = Session["LastName"].ToString();

            string email = Session["E-mail"].ToString();

            Response.Write(string.Format("User First name is :{0}<br />", firstname));

            Response.Write(string.Format("User Last name is :{0}<br />", lastname));

            Response.Write(string.Format("User E-mail  is :{0}", email));

        }

        catch (NullReferenceException ex)

        {

            Response.Write(ex.Message + " Becasue Session doesn't exist");

        }

    }

 
Step 4


Run the InprocSessionExample web application:

In-proc session state in asp.net

If the server will be closed then the inproc session state mode store value will be

lost.

In-proc session state in asp.net

Advantage of in-proc session state mode


·         Easy to implement

·         Stores the session data on the server so it is fast. 


Disadvantage of in-proc session state mode

·     When the application domain or worker process recycles, the session data

will be lost.

·      It is usually the fastest, but more session data means more memory is used

on the web server, and that can affect performance


Updated 07-Sep-2019

Leave Comment

Comments

Liked By