Example Of StateServer Session Mode in asp.net

Here is one simple example of  using StateServer Session mode. I have created this sample web application directly on the IIS so that we can easily understand its usage. 
Step 1 :  Open Visual Studio > File > New > Web Sites . Choose Location as HTTP and create the web application .



Now if you open the IIS you will see a Virtual Directory created with the name of your web application , as in my case it is StateServer.






Step 2 :  Create s simple UI that will take Roll No and Name of a student . We will store the name and roll in a state server session. I have also create one same class "StudentInfo"  .  This class is given below

 [Serializable]
public class StudentInfo
{
    //Default Constructor
    public StudentInfo()
    {
      
    }
    /// <summary>
         /// Create object of student Class
         /// </summary>
         /// <param name="intRoll">Int RollNumber</param>
         /// <param name="strName">String Name</param>
    public StudentInfo(int intRoll, string strName)
         {
        this.Roll = intRoll;
        this.Name = strName;
         }

    private int intRoll;
    private string strName;
    public int Roll
    {
        get
        {
            return intRoll;
        }
        set
        {
            intRoll = value;
        }
    }

    public string Name
    {
        get
        {
            return strName;
        }
        set
        {
            strName = value;
        }
    }
}
Now , have a look on the code behind code. I have just added two button one for storing session and another for retrieving session.

protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
        StudentInfo _objStudentInfo = new StudentInfo(Int32.Parse( txtRoll.Text) ,txtUserName.Text);
        Session["objStudentInfo"] = _objStudentInfo;
        ResetField();
    }
    protected void btnRestore_Click(object sender, EventArgs e)
    {
        StudentInfo _objStudentInfo = (StudentInfo) Session["objStudentInfo"];
        txtRoll.Text = _objStudentInfo.Roll.ToString();
        txtUserName.Text = _objStudentInfo.Name;
       
    }
Step 3 : Please Configure your web.config for state server. As I have already discussed.  And Please make sure aspnet_state.exe is up and running on that configured server.
Step 4 : Run the Application




Enter the data, Click on Submit.
Now there are following Test that I have made which will totally clear your doubts that how exactly StateServer is useful. 
First :Remove the [ Serializable ] key word from the studentinfo class and try to run the application. When you will click on Submit Button you will get following error



Which clearly says that you should have to serialize the object before store.
Second: Run the Application, Store data by clicking on Submit Button. Restart IIS



Now, In case of InProc, you have already lost your session data, But Its StateServer, Click on Restore Session, You will get your original data. Because State server data does not depend on IIS. Its keeps it separately.
Third : Stop the aspnet_state.exe from the Windows Services MMC and Submit the Data. You will get following error,



Because your State Server Process is not running.
So, Please keep in mind about those three points .

0 comments:

Post a Comment