Web Services Using C# - Chapter 5 - Sessions In Web Services

In order to enable Sessions in Web Services, we have to follow the following three steps:

  1. Import “System.Web.Services.WebServices”.
  2. Enable the web method attribute as EnableSession=true.
  3. At ClientSide, enable the allowcookies=true in web.config file.

In Web Service Code: (set EnableSession = true)

  1. public class MyWebService: System.Web.Services.WebService {  
  2.     [WebMethod(MessageName = "AddTwoNumbers", Description = "Add Two Numbers; Paratmeters: double firstNumber, double secondNumber", EnableSession = true)]  
  3.     public string AddTwoNumbers(double firstNumber, double secondNumber) {  
  4.             List < string > lstAddedTwoNumbers;  
  5.             if (Session["AddedTwoNumbers"] == null) {  
  6.                 lstAddedTwoNumbers = new List < string > ();  
  7.             } else {  
  8.                 lstAddedTwoNumbers = (List < string > ) Session["AddedTwoNumers"];  
  9.             }  
  10.             lstAddedTwoNumbers.Add(string.Format("{0} + {1} = {2}", firstNumber.ToString(), secondNumber.ToString(), (firstNumber + secondNumber)));  
  11.             Session["AddedTwoNumbers"] = lstAddedTwoNumbers;  
  12.             return Convert.ToString(firstNumber + secondNumber);  
  13.         }  
  14.         [WebMethod(MessageName = "AddThreeNumbers", Description = "Add three numbers; Paratmeters: double firstNumber, double secondNumber, double thirdNumber", EnableSession = true)]  
  15.     public string AddThreeNumbers(double firstNumber, double secondNumber, double thirdnumber) {  
  16.             return Convert.ToString(firstNumber + secondNumber + thirdnumber);  
  17.         }  
  18.         [WebMethod(Description = "Get All the numbers added with AddTwoNumbers", EnableSession = true)]  
  19.     public List < string > GetAddedNumbersFromSession() {  
  20.         if (Session["AddedTwoNumbers"] != nullreturn (List < string > ) Session["AddedTwoNumbers"];  
  21.         else {  
  22.             List < string > data = new List < string > ();  
  23.             data.Add("No record found");  
  24.             return data;  
  25.         }  
  26.     }  
  27. }  
 

In ASP.NET Client Code:

(set allowCookies=”true”)

  1. <?xml version="1.0"?>  
  2. <!--  
  3.   
  4. For more information on how to configure your ASP.NET application, please visit  
  5.   
  6. http://go.microsoft.com/fwlink/?LinkId=169433  
  7.   
  8. -->  
  9. <configuration>  
  10.     <system.web>  
  11.         <compilation debug="true" targetFramework="4.5">  
  12.             <assemblies>  
  13.                 <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>  
  14.         </compilation>  
  15.         <httpRuntime targetFramework="4.5" executionTimeout="1000" />  
  16.         <sessionState timeout="1000" /> </system.web>  
  17.     <system.serviceModel>  
  18.         <bindings>  
  19.             <basicHttpBinding>  
  20.                 <binding name="MyWebServiceSoap" allowCookies="true" sendTimeout="00:10:00" receiveTimeout="00:10:00" /> </basicHttpBinding>  
  21.         </bindings>  
  22.         <client>  
  23.             <endpoint address="http://localhost:1810/MyWebService.asmx" binding="basicHttpBinding" bindingConfiguration="MyWebServiceSoap" contract="ServiceReference1.MyWebServiceSoap" name="MyWebServiceSoap" /> </client>  
  24.     </system.serviceModel>  
  25. </configuration>  
 

Add a GridView to view session details, 
Modify code to view the result.

  1. protected void btnAddTwoNumbers_Click(object sender, EventArgs e) {  
  2.     ServiceReference1.MyWebServiceSoapClient client = new ServiceReference1.MyWebServiceSoapClient();  
  3.     lblResult1.Text = String.Format("Answer: {0} + {1} = {2}", txtfirstNumber1.Text, txtsecondNumber1.Text, client.AddTwoNumbers(Convert.ToDouble(txtfirstNumber1.Text), Convert.ToDouble(txtsecondNumber1.Text)));  
  4.     grdAllResults1.DataSource = client.GetAddedNumbersFromSession();  
  5.     grdAllResults1.DataBind();  
  6. }  

Output