Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 3 - Using ASP.NET Session State in a Web Service

Suggested Videos
Part 1 - Introduction to asp.net web services
Part 2 - Consuming a web service



In this video, we will discuss using asp.net session variables in a web service. This is continuation to Part 2. Please watch Part 2 from the ASP.NET tutorial before proceeding.

To use asp.net session object in a web service, the web service class must inherit from System.Web.Services.WebService class and EnableSession property of WebMethod attribute must be set to true.



Let us now use Session object with the Calculator Service that we have built in Part 2. We want to display all the calculations that a user has performed as shown below. Here are the steps to achieve this.
Step 1: Copy and paste the following code in CalculatorWebServices.asmx file
using System.Web.Services;
using System.Collections.Generic;

namespace WebServicesDemo
{
    [WebService(Namespace = "http://pragimtech.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class CalculatorWebServices : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public int Add(int firstNumber, int secondNumber)
        {
            List<string> calculations;

            if (Session["CALCULATIONS"] == null)
            {
                calculations = new List<string>();
            }
            else
            {
                calculations = (List<string>)Session["CALCULATIONS"];
            }
            
            string strTransaction = firstNumber.ToString() + " + " 
                + secondNumber.ToString() 
                + " = " + (firstNumber + secondNumber).ToString();
            calculations.Add(strTransaction);
            Session["CALCULATIONS"] = calculations;

            return firstNumber + secondNumber;
        }

        [WebMethod(EnableSession = true)]
        public List<string> GetCalculations()
        {
            if (Session["CALCULATIONS"] == null)
            {
                List<string> calculations = new List<string>();
                calculations.Add("You have not performed any calculations");
                return calculations;
            }
            else
            {
                return (List<string>)Session["CALCULATIONS"];
            }
        }
    }
}

Step 2: The next step is to update the proxy class in the client application, that is in the CalculatorWebApplication. To achieve this, right click on CalculatorService in Service References folder and select Update Service Reference option.

Step 3: Copy and paste the following HTML in WebForm1.aspx
<table style="font-family: Arial">
<tr>
    <td>
        <b>First Number</b>
    </td>
    <td>
        <asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td>
        <b>Second Number</b>
    </td>
    <td>
        <asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td>
        <b>Result</b>
    </td>
    <td>
        <asp:Label ID="lblResult" runat="server"></asp:Label>
    </td>
</tr>
<tr>
    <td colspan="2">
        <asp:Button ID="btnAdd" runat="server" Text="Add" 
        OnClick="btnAdd_Click" />
    </td>
</tr>
<tr>
    <td>
        <asp:GridView ID="gvCalculations" runat="server">
        </asp:GridView>
    </td>
</tr>
</table>

Step 4: Copy and paste the following code in WebForm1.aspx.cs
using System;

namespace CalculatorWebApplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            CalculatorService.CalculatorWebServicesSoapClient client = 
                new CalculatorService.CalculatorWebServicesSoapClient();

            int result = client.Add(Convert.ToInt32(txtFirstNumber.Text), 
                Convert.ToInt32(txtSecondNumber.Text));
            lblResult.Text = result.ToString();

            gvCalculations.DataSource = client.GetCalculations();
            gvCalculations.DataBind();

            gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";
        }
    }
}

Step 5: In web.config file of CalculatorWebApplication, set allowCookies attribute to true
<basicHttpBinding>
  <binding allowCookies="true" name="CalculatorWebServicesSoap" />
</basicHttpBinding>

When allowCookies attribute is set to true, the client application accepts the cookie returned from the ASMX web service, and copies it into all future requests that are made to the web service. This ensures that the same session is maintained between the client and the web service.

asp.net webservices tutorial

5 comments:

  1. In Step 5 when I open the Web.config file I don't have neither do I have all the other bindings represented in your video. Yet, I tried adding the line of code in Step 5 '' and it still doesn't work.
    I am using Visual Studio 2015 Community and has been following your video tutorials step by step. Any help?

    ReplyDelete
    Replies
    1. you should select .Net 4.0 when devolping your project to work it correctly

      Delete
  2. what is gvCalculations?

    ReplyDelete
  3. gvCalculations is the GridView control name, created in step 3.

    ReplyDelete
  4. Getting null reference error when doing second calculation>
    Help please

    ReplyDelete

It would be great if you can help share these free resources