Using ASP.NET Session State in a Web Service:-
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.

Step(1):-Create a Web Service name is CalculateWebServices and copy and paste the following code as shown in below.

[WebService(Namespace = "http://santosh-asp.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]


    public class Service1 : 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):-Create the client application named is CalculateWebServiceClient in ASP.NET Website and add a Default.aspx page.

Step(3):-Now, copy and paste the following code in Default.aspx page.

  


<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 the code in Default.aspx.cs

  protected void btnAdd_Click(object sender, EventArgs e)
        {
            ServiceReference1.Service1SoapClient objService = new ServiceReference1.Service1SoapClient();
           int result  = objService.Add(Convert.ToInt32(txtFirstNumber.Text),
                Convert.ToInt32(txtSecondNumber.Text));
           lblResult.Text = result.ToString();


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

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


Step(5):- In web.config file of CalculateWebServices, set allowCookies attribute to true.

  

<bindings>
            <basicHttpBinding>
                <binding allowCookies="true" name="Service1Soap" />
            </basicHttpBinding>
        </bindings>




Step(7):-Now, run the application and get output as our requirement.