Simple Web service application, creation and calling process (image and text)

Source: Internet
Author: User

(1) create a WebService Application
First, open vs2005, open "file-New-Website", and select "ASP. NET web service"

 

View service. CSCode, You will find that vs. NET 2005 has established a default framework for the Web service file. The original code is:


Using System; Using System. LINQ; Using System. Web; Using System. Web. Services; Using System. Web. Services. Protocols; Using System. xml. LINQ; [WebService (namespace =" Http://tempuri.org/ ")] [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream. // [System. Web. Script. Services. scriptservice] Public Class Service: system. Web. Services. WebService { Public Service (){ // If you use the designed component, uncomment the following line // Initializecomponent (); } [Webmethod] Public String Helloworld (){ Return " Hello World ";}}

You can directly run service. asmx to view the effect as follows:

Click it to call the default helloworld method and return an XML result, indicating that our simple WebService environment is normal.

 

Now, we manually add a simple Summation Method to the service. CS class. The Code is as follows:

 Using System; Using System. LINQ; Using System. Web; Using System. Web. Services; Using System. Web. Services. Protocols; Using System. xml. LINQ; [WebService (namespace =" Http://tempuri.org/ ")] [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.  // [System. Web. Script. Services. scriptservice]  Public   Class Service: system. Web. Services. WebService { Public Service (){ // If you use the designed component, uncomment the following line          // Initializecomponent (); } [Webmethod] Public   String Helloworld (){ Return " Hello World ";} [Webmethod (description ="Sum of two integers ")] Public   Int Sum ( Int A, Int B ){ Return A + B ;}}

Note that if we write a function and want it to become an externally callable interface function, we must add a line of code [webmethod (description = "function description")] to the function. If your function does not have this Declaration, it cannot be referenced by users.
NOTE 1: The description = "function description" section can be omitted without affecting the method to be called. However, a brief introduction of the method is usually provided.
Note 2: The service class can also contain methods that are not called by the outside world. Do not confuse them.

The effect after running is as follows:

Click sum:

After the values of A and B are input and called, The results page in XML format is returned.

Now, a simple WebService application has been created. You can call it by publishing it on the server.

 

 

(2) WebService application call
Create a winform ApplicationProgramOr ASP. NET applications, WebService calling is very simple, steps and points are as follows:

  1. Publish the created web application on the server. For example, configure the new web service on the local IIS ,:
  2. Right-click the project in resource manager, select Add web reference, find the target service, and click Add application.

  3. After successful addition, in the project:
  4. You must instantiate the Web service before calling the web method. in this example, the background code calls the web method as follows:
      
    Public partial class _ default: system. web. UI. page { protected void page_load ( Object sender, eventargs e) {btndo. click + = New eventhandler (btndo_click) ;}< span style = "color: # 0000ff; "> void btndo_click ( Object sender, eventargs e) {localhost. service S = New localhost. service (); txtresult. TEXT = S. sum (convert. toint32 (txtnum1.text), convert. toint32 (txtnum2.text )). tostring () ;}

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.