Asp. NET how to use session state for Web services _ Practical Tips

Source: Internet
Author: User

There are 2 things you need to do to use a session object in ASP.net in a Web service.

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

The value of the EnableSession property in 2.WebMethod should be set to True

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

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

To do this, first consider modifying the Add method of the Calculatorwebservice class, as follows.

[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 + second Number). ToString ();
   Calculations. ADD (strtransaction);
   session["calculations"] = calculations;
 
   return firstnumber + secondnumber;
  }

Then another public method is introduced to return all the calculated results. To use the WebMethod attribute to decorate this method, and set the EnableSession property to True.

[WebMethod (EnableSession = true)]
  Public list<string> getcalculations ()
  {
   if (session["calculations"] = = null)
   {
    list<string > calculations = new list<string> ();
    Calculations. ADD ("have not performed any calculations");
    return calculations;
   }
   else
   {return
    (list<string>) session["calculations"];
   }
  


Now we can build our solution and look up our web services in our browsers.

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

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

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

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

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

<tr>
 <td>
  <asp:gridview id= "gvcalculations" runat= "Server" >
  </asp:GridView>
 </td>
</tr>

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

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

Gvcalculations.datasource = client. Getcalculations ();
   Gvcalculations.databind ();
 
   Gvcalculations.headerrow.cells[0]. Text = "recent calculations";

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

Let's continue to add two numbers, such as 20 and 30. And we'll see that even though we've performed a calculation, messages like have not performed any calculations will 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 we're going to run this web window and add some numbers. Now we can see it running as expected.

So, here are a few things to think about:

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

Set the AllowCookies property to True so that the client application accepts the cookie returned from the ASMX Web service and copies it to all future requests originating from the Web service. This ensures that the same session is maintained between the client and the Web service.

How to use the session state of the Web service, I believe that through this article you should have some understanding of it.

Related Article

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.