WebService Study Notes

Source: Internet
Author: User

What's the use of webservice?

Let's start with a brief introduction to WCF. When developing with WebService, many people do not know the relationship between WCF and WebService. In fact, WCF contains the webservice, which is a very powerful framework for communication technology application. Microsoft has fused all of the technologies involved in distributed communications into WCF in the. NET platform. For example, WebService has converged to WCF , MSQ( Microsoft Message Queuing ), Even some communication protocols, such as TCP and UDP, are also fused . So WCF solves the communication problem in the. NET platform.

So you may not be able to understand it, so let's simulate how to solve a cross-system, cross-platform, cross-server communication scenario (for example).

The nature of WebService: a problem that can be used to solve communications--------------------------------------------------------------------

Requirements: Let the three companies under the system can communicate, such as I use C # to write a computational subtraction program to let Java or PHP system directly to use it.

This is undoubtedly an incomprehensible question. At this time we can try to use WebService to deal with this puzzling thing.

How do I create and use WebService?

Steps:

1. Add a Web Service


2. Once added, you can see that a. asmx file is generated and we need to look at the resulting. Asmx.cs background page. In. NET3.5 or earlier versions, this CS page is typically automatically generated into the App_Code folder. Of course. NET4.0 (contains a later version) is generated as a node. Here's a look at the code structure of this file:

    /// <summary>    ///Summary description of Webservicedemo/// </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 classWebServiceDemo:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello"; }    }

This code is actually very simple, a common class to inherit System.Web.Services.WebService this base class.

The [WebMethod] property attaches a method to the public method to indicate that you want to expose the method as part of an XML Web services. The popular saying is that when you do not add this attribute, the webservice you write cannot be accessed externally.

3. Create a simple WebService

 Public int ADD (intint  b)  {     return a + b;}

4. Reference: Call it through the WinForm program

4.1 After you create the WinForm program, add the service reference

4.2 Put the WebService address to the address to search for the service so you can see the method you just created

5. Enable the WinForm program you just created to invoke this program

Private void button1_click (object  sender, EventArgs e) {    intint. Parse (t1. Text);     int int . Parse (T2. Text);     New Var_webservice.webservicedemosoapclient (); // WebService is the SOAP protocol.    int result= client. Add (A, b);    MessageBox.Show (result. ToString ());}

Results:

WebService Study Notes

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.