1. Understanding: WebService is to achieve communication between different applications on different servers
2, let us step-by-step to do a webservice simple application
1) Create a new empty Web application, right-click on the program, create a new project, select "Web Service", a weservice1.asmx program will appear, the program is as follows:
namespacewebapplication1{/// <summary> ///Summary description of WebService1/// </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 classWebService1:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello World"; } }}
2) We can add our own method: The following Add method:
1 Public classWebService1:System.Web.Services.WebService2 {3 4 [WebMethod]5 Public stringHelloWorld ()6 {7 return "Hello World";8 }9 [WebMethod]Ten Public intADD (intAintb) { One returnA +b; A } -}
3) We build an empty Web application, using this Web application to remove the webservice we built in the above method, we are the simulation of the same host of different application communication, in fact, different host different applications are the same:
4) Right-click on the reference of the second new application, "Add Service Reference", point "discover", and in the "Services" window will appear the webservice we have built, choose OK
5) We create a new Web Form in the second new application, call the Add method in the WebService, open it in the browser and then output the result 7 after we call it:
1 protected void Page_Load (object sender, EventArgs e)2 {3 new servicereference1.webservice1soapclient (); 4 int sum=client. ADD (3,4); 5 Response.Write (sum); 6 }
WebService dome--A simple small example of a webservice