Click here to Skip to main content
13,597,797 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 14 Aug 2014
Licenced CPOL

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

Rate this:
Please Sign up or sign in to vote.
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.


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>();
                calculations = (List<string>)Session["CALCULATIONS"];
            string strTransaction = firstNumber.ToString() + " + " 
                + secondNumber.ToString() 
                + " = " + (firstNumber + secondNumber).ToString();
            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;
                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.

        <asp:GridView ID="gvCalculations" runat="server">


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.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)


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


About the Author

Arun Ramachandran India
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

You may also be interested in...


Comments and Discussions

QuestionIt is really helpful Pin
NDEREBEZA30-Jul-16 4:00
memberNDEREBEZA30-Jul-16 4:00 
QuestionNice Pin
Júnior Pacheco14-Aug-14 15:26
memberJúnior Pacheco14-Aug-14 15:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 14 Aug 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid