WebService dome--A simple small example of a webservice

Source: Internet
Author: User

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

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.