In the last blog post, we have discussed about consuming web services from the client application. In this article we will go over how to use session state in a web service.
This is continuation of the previous article. So please go through that before proceeding this article to get a clear idea. You can read that article here.
To use ASP.NET Session object in a web service, there are 2 things we need to do.
- The WebService class should inherit from
System.Web.Services.WebService
class. - The
EnableSession
property of the WebMethod attribute should be set to true.
While looking at our CalculatorWebService
class, we can see that it is already inherited from System.Web.Services.WebService
class. But we need to set EnableSession
property to true.
In this article, we will be trying to display the recent calculations using a session object in a GridView like below.
To achieve this, first of all, modify the Add
method of CalculatorWebService
class like below.
[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;
}
Then include another public method to return all the calculations. Decorate this method with WebMethod
attribute and set EnableSession
property to true.
[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"];
}
}
Now build the solution and view our web service in a browser.
The web service should list 2 methods – Add
and GetCalculations
.
Click on the Add
method. Let’s add 2 numbers, say 20 and 30. While clicking on the Invoke
button, we will get the result as 50.
Let’s do another calculation, say 30 and 70. While clicking on the Invoke
button, we will get the result as 100.
Now let’s go back and test our GetCalculations
method. While clicking on the Invoke
button, all the calculations we have performed until now are displayed. They will be returned as an array of strings.
So our web service is working as expected. Now let’s try to use these methods in our client web application. For that, within the Webform1.aspx, let’s drag and drop a GridView control.
<tr>
<td>
<asp:GridView ID="gvCalculations" runat="server">
</asp:GridView>
</td>
</tr>
Before the code behind file modifications, we need to update the proxy class. To do this, right click on the CalculatorService
and select Update Service Reference.
After that, within the btnAdd_Click
event, add the following lines of codes.
gvCalculations.DataSource = client.GetCalculations();
gvCalculations.DataBind();
gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";
Build the solution and view the webform in a browser.
Let’s go ahead and add 2 numbers, say 20 and 30. But even though we have performed one calculation, a message of You have not performed any calculations will be displayed.
This is basically because the web application is not sending the same SessionId to the web service. To make it work, set allowCookies
to true in web.config file.
Let’s run the webform once again and add some numbers. Now we can see that this is working as expected.
So points here to ponder are following:
- If web service has changed, the proxy class in the client application needs to be updated. To do this, right click on the service in Service Reference folder and select Update Service Reference option.
- Set
allowCookies
attribute to true
for the client application to accepts the cookie returned from the ASMX web service and to copy 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.
What’s next?
In the next article, we will discuss about WebMethod attribute properties.
Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)