WebService synchronous invocation, asynchronous invocation

Source: Internet
Author: User

Read Catalogue

  One: Add WebService Service

Two: Add "client program" (here with WinForm) to invoke the WebService service

Three: Asynchronous call procedure explanation

One: Add WebService service

1, add an empty website project, 2, add a WebService service (webservicetest.asmx) in the project, 3, add "HelloWorld" method

Code: (webservicetest.asmx)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading;usingsystem.web;usingSystem.Web.Services;namespacet1_webservice{/// <summary>    ///Summary description of WebServiceTest/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]    //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService]     Public classWebServiceTest:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld (intAintb) {//wait 5 secondsThread.Sleep ( the); return(A +b).        ToString (); }    }}
View Code

Two: Add "client program" (here with WinForm) to invoke the WebService service

1. Add a WinForm project.

2. Add "Referral Service (WebService service)"

3. Add a WinForm form

  

4. The code to write the call in Btnadd is as follows

        Private voidButton1_Click (Objectsender, EventArgs e) {servicereferencetest.webservicetestsoapclient Client=Newservicereferencetest.webservicetestsoapclient (); intA =int.            Parse (Tbxa.text); intb =int.            Parse (Tbxb.text); //Synchronous Invocation//tbxresult.text = client.            HelloWorld (A, b); //The following is an asynchronous call//Registering callback EventsClient. helloworldcompleted + =client_helloworldcompleted; //Calling Async Methodsclient.                    Helloworldasync (A, b); }        voidClient_helloworldcompleted (Objectsender, Servicereferencetest.helloworldcompletedeventargs e) {            //callback Event Trigger, return resultTbxresult.text =E.result; }
View Code

PS: There are synchronous calls (commented out) and asynchronous calls.

When calling the WebService service for a long time, you can use asynchronous calls to do the main thread without the card.

WebService synchronous invocation, asynchronous invocation

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.