Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

ASP.Net Web Services – How to use session state in a web service?

4.60/5 (5 votes)
14 Aug 2014CPOL3 min read 46.2K  
CodeProject 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.

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.

  1. The WebService class should inherit from System.Web.Services.WebService class.
  2. The EnableSession property of the WebMethod attribute should be set to true.

WebService1

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.

Purpose

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;
        }

WebService2

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"];
            }
        }

WebService3

Now build the solution and view our web service in a browser.

WebService4

The web service should list 2 methods – Add and GetCalculations.

WebService5

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.

WebService6

WebService7

Let’s do another calculation, say 30 and 70. While clicking on the Invoke button, we will get the result as 100.

WebService8

WebService9

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.

WebService10

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>

WebService11

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.

WebService12

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";

WebService13

Build the solution and view the webform in a browser.
WebService14

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.

WebService15

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.

WebService16

Let’s run the webform once again and add some numbers. Now we can see that this is working as expected.

WebService17

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)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)