In order to enable Sessions in Web Services, we have to follow the following three steps:
- Import “System.Web.Services.WebServices”.
- Enable the web method attribute as EnableSession=true.
- At ClientSide, enable the allowcookies=true in web.config file.
In Web Service Code: (set EnableSession = true)
- public class MyWebService: System.Web.Services.WebService {
- [WebMethod(MessageName = "AddTwoNumbers", Description = "Add Two Numbers; Paratmeters: double firstNumber, double secondNumber", EnableSession = true)]
- public string AddTwoNumbers(double firstNumber, double secondNumber) {
- List < string > lstAddedTwoNumbers;
- if (Session["AddedTwoNumbers"] == null) {
- lstAddedTwoNumbers = new List < string > ();
- } else {
- lstAddedTwoNumbers = (List < string > ) Session["AddedTwoNumers"];
- }
- lstAddedTwoNumbers.Add(string.Format("{0} + {1} = {2}", firstNumber.ToString(), secondNumber.ToString(), (firstNumber + secondNumber)));
- Session["AddedTwoNumbers"] = lstAddedTwoNumbers;
- return Convert.ToString(firstNumber + secondNumber);
- }
- [WebMethod(MessageName = "AddThreeNumbers", Description = "Add three numbers; Paratmeters: double firstNumber, double secondNumber, double thirdNumber", EnableSession = true)]
- public string AddThreeNumbers(double firstNumber, double secondNumber, double thirdnumber) {
- return Convert.ToString(firstNumber + secondNumber + thirdnumber);
- }
- [WebMethod(Description = "Get All the numbers added with AddTwoNumbers", EnableSession = true)]
- public List < string > GetAddedNumbersFromSession() {
- if (Session["AddedTwoNumbers"] != null) return (List < string > ) Session["AddedTwoNumbers"];
- else {
- List < string > data = new List < string > ();
- data.Add("No record found");
- return data;
- }
- }
- }
In ASP.NET Client Code:
(set allowCookies=”true”)
- <?xml version="1.0"?>
- <!--
-
- For more information on how to configure your ASP.NET application, please visit
-
- http:
-
- -->
- <configuration>
- <system.web>
- <compilation debug="true" targetFramework="4.5">
- <assemblies>
- <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>
- </compilation>
- <httpRuntime targetFramework="4.5" executionTimeout="1000" />
- <sessionState timeout="1000" /> </system.web>
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="MyWebServiceSoap" allowCookies="true" sendTimeout="00:10:00" receiveTimeout="00:10:00" /> </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:1810/MyWebService.asmx" binding="basicHttpBinding" bindingConfiguration="MyWebServiceSoap" contract="ServiceReference1.MyWebServiceSoap" name="MyWebServiceSoap" /> </client>
- </system.serviceModel>
- </configuration>
Add a GridView to view session details,
Modify code to view the result.
- protected void btnAddTwoNumbers_Click(object sender, EventArgs e) {
- ServiceReference1.MyWebServiceSoapClient client = new ServiceReference1.MyWebServiceSoapClient();
- lblResult1.Text = String.Format("Answer: {0} + {1} = {2}", txtfirstNumber1.Text, txtsecondNumber1.Text, client.AddTwoNumbers(Convert.ToDouble(txtfirstNumber1.Text), Convert.ToDouble(txtsecondNumber1.Text)));
- grdAllResults1.DataSource = client.GetAddedNumbersFromSession();
- grdAllResults1.DataBind();
- }
Output