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