ASP. NET Web Service – how session state is used

Source: Internet
Author: User

In the last blog post, we discussed the client's use of Web services. In this article we will review how to use the session state of the Web service.

This is a continuation of the previous article. So please review the previous article quickly so that there is a clear concept.

There are 2 things to do in a Web service that uses the Session object in ASP.

The 1.WebService class needs to inherit the System.Web.Services.WebService class

The EnableSession attribute value in 2.WebMethod should be set to True

To see our Calculatorwebservice class, we can see that it has inherited the System.Web.Services.WebService class. However, we need to set the EnableSession property value to True.

In this article, we'll try to show the most recent results using a Session object in the GridView as shown below.

In order to achieve this, the first thing to do is to modify the Add method of the Calculatorwebservice class.

12345678910111213141516171819202122 [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 another public method is introduced to return all the computed results. To use the WebMethod attribute to decorate this method, and set the EnableSession property to True.

1234567891011121314 [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 it's time to build our solution and see our Web service in our browser.

The Web service lists two methods--add and getcalculations.

Click the Add method. Let's enter two numbers, such as 20 and 30, and then click the Invoke button, and we'll get the result of 50.

Let's do another calculation, like 30 and 70. Then click the Invoke button and we will get the result as 100.

Now let's go back and test our getcalculation method. Then click the Invoke method and now show all the calculations we've done before. They are returned as an array of strings.

So our web service works as expected. Now let's try to use these methods in our Web application. To do this, in WebForm1.aspx, let's drag a GridView control into it.

123456 <tr>    <td>        <asp:GridView ID="gvCalculations" runat="server">        </asp:GridView>    </td></tr>

Before the file is modified, we need to update the proxy class. To do this, in CalculatorService and select Update Service Reference.

Then, in the btnAdd_Click event code snippet, add the following lines of code.

1234 gvCalculations.DataSource = client.GetCalculations();            gvCalculations.DataBind();            gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";

Build our solution and view the web window in the browser.

Let's continue adding two numbers, such as 20 and 30. And we will see that even though we have performed a calculation, you will not be performed any calculations such a message would still be displayed.

This is basically because the Web application does not send the same sessionid as the Web service. To do this, set the Allowcookie in the Web. config file to true.

Now let's run the web window and add some numbers. Now we can see that it's working as expected.

So, here are a few things to think about:

    • If the Web service is modified, the proxy class for the client application is updated. To do this, right-click on the service below the Services Reference folder and select the Update Service reference item.

    • Set the AllowCookies property to True to allow the client application to accept the cookie returned from the ASMX Web service and copy it to the requests originating from all future Web services. This ensures that the same session is maintained between the client and the Web service.

What's next?

In subsequent articles, we will discuss what WebMethod attributes and their attributes.

Reference: Arun Ramachandran (Http://BestTEchnologyBlog.Com)

Original address: Http://www.codeproject.com/Articles/807843/ASP-Net-Web-Services-How-to-use-session-state-in-a

ASP. NET Web Service – how session state is used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.