. The development of Web service in net

Source: Internet
Author: User
Tags command line html form soap wsdl
Web. NET platform to support Web service, including the construction and use of Web service. Unlike other development platforms, use. NET platform, you do not need other tools or SDK to complete the development of Web service. The. NET framework itself fully supports Web service, including server-side request processors and support for sending and receiving SOAP messages to clients. This section will take you with you. NET to create and use a simple Web service.
To be in. NET to create a Web service, you just need to create an. asmx file. This file has a WebService tag that contains the language and class two attributes, which are used to specify the classes exposed in the programming language and Web service, respectively. Then you can write your class as usual. Finally, add a System.Web.Services.WebMethodAttribute attribute to each of the methods you want to expose. The final code is similar to the following list of programs.
' A WebService in vb.net (calc_vb.asmx)
<% @WebService language= "VB" class= "Calc"%>
Imports System.Web.Services
Public Class Calc
<webmethod () > _
Public Function Add (ByVal A as Double, _
ByVal B as double) as double
Return a + b
End Function
End Class


Browse the. asmx file in a browser and you'll get a page to test the Web service. For example, if you put the Calc_vb.asmx file in the MyService directory of the Web server, the corresponding URL is:
Http://localhost/myService/calc_vb.asmx
The test page is shown in the following figure. This page is automatically generated. It displays the name of the Web service and lists the methods that can be invoked. There is a text behind the list that means you are using the default namespace http://tempuri.org/, and if you want to publish the Web service, it's best to change your own namespace to avoid conflicting names. The question of the Web service namespace will be discussed later in this book. In the sixth chapter, I will also introduce how to use. NET to specify namespaces and other things.



Auto-generated Web Service test page

Click the Add method on the test page and you will get an HTML form to test the method (see below). In this form, all parameters accepted by the Add method have a corresponding text box. Fill in all the parameters and click the "Invoke" button, and the form will be submitted to the Web server. In fact, this is the invocation of the Web service in the form of HTTP GET. The resulting result is a simple XML document as follows:
<double xmlns= "http://tempuri.org/" >158</double>
Browse to the following URL to call the Add method directly:
Http://localhost/myService/calc_vb.asmx/Add?a=123&b=35
As you can see, the name of the method is the resource you requested (note that this is case-sensitive), and each parameter in the function is mapped to a parameter in the query string. This form is very handy for fast testing a Web service. However, because HTTP GET is used in this way, it has some limitations in terms of data type and parameter passing direction. We will discuss these limitations in detail in chapter sixth.



Automatically generated Add method test page

Back to the previous Web Service test page, we can also see a service description link at the top of the page. Click in the past to get a WSDL document describing the Web service, as shown below. After the Web service URL is followed by a "WSDL" query string, you can also browse to the page directly:
http://localhost/myService/calc_vb.asmx?wsdl





The WSDL document for the Calc Web service. Note that all of the XML elements are folded to show more content.
The WSDL document for the Calc Web service. Note that all of the XML elements are folded to show more content.
To be in. NET to invoke a Web service, you need to run the Wsdl.exe tool first. The tool reads its WSDL description document from the Web service and generates a proxy class that can invoke the Web service. For example, you can generate a proxy class for the Calc Web service by executing the following command at the command line:
WSDL.EXE/LANGUAGE:VB http://localhost/myService/calc_vb.asmx?wsdl
The program listing 1-2 intercepts the generated VB proxy class source code. This proxy class inherits from the System.Web.Services.Protocols.SoapHttpClientProtocol class and exposes an Add method that receives two double floating-point numbers and returns a double floating-point number.
Program Listing 1 2 intercepts the generated Web service proxy class source code from Wsdl.exe
Imports System.Web.Services.Protocols
' Omit other code
Public Class Calc
Inherits SoapHttpClientProtocol
' Omit other code
<system.diagnostics.debuggerstepthroughattribute (), _
Soapdocumentmethodattribute (_
"Http://tempuri.org/Add", _
Use:=system.web.services.description.soapbindinguse.literal,_
parameterstyle:= soapparameterstyle.wrapped) >_
Public Function Add (ByVal A as double, byvalb as double) as double
Dim results () as Object = Me.invoke ("Add", _
New Object () {A, b})
Return CType (Results (0), Double)
End Function
' Omit other code
End Class
The work since then is very simple. To invoke the Calculatorweb service, simply instantiate a Calc proxy object and call its Add method:
Dim ws as New Calc ()
Dim result as Double = ws. ADD (20.5, 10.9)
MessageBox.Show ("The result is:" &result.) ToString)


In fact, the Add method in the proxy class simply invokes the Web service through the SoapHttpClientProtocol class of the. NET framework, and then returns the return value of the Web service to the caller.

Of course, there is much more to the creation and invocation of. NET Web service than the above demo. Before delving into these details, however, we need to first understand several key techniques in WebService: XSD, SOAP, and WSDL.




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.