Of course, you shouldn’t
have to build HTTP or SOAP requests by hand. And indeed, you
don’t; the .NET Framework SDK includes a tool,
wsdl.exe
, which can generate web service client
code from any WSDL file.
Run the command line wsdl /language:vb
http://127.0.0.1/dotNetAndXml/InventoryQuery.asmx?WSDL
to
produce the Visual Basic .NET source code listed in Example 10-7 for the InventoryQuery
service.
'------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated by a tool. ' Runtime Version: 1.0.3705.288 ' ' Changes to this file may cause incorrect behavior and will be lost if ' the code is regenerated. ' </autogenerated> '------------------------------------------------------------------------------ Option Strict Off Option Explicit On Imports System Imports System.ComponentModel Imports System.Diagnostics Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Xml.Serialization ' 'This source code was auto-generated by wsdl, Version=1.0.3705.288. ' '<remarks/> <System.Diagnostics.DebuggerStepThroughAttribute( ), _ System.ComponentModel.DesignerCategoryAttribute("code"), _ System.Web.Services.WebServiceBindingAttribute(Name:="InventoryQuerySoap", [Namespace]:="http://angushardware.com/InventoryQuery")> _ Public Class InventoryQuery Inherits System.Web.Services.Protocols.SoapHttpClientProtocol '<remarks/> Public Sub New( ) MyBase.New Me.Url = "http://127.0.0.1/dotNetAndXml/InventoryQuery.asmx" End Sub '<remarks/> <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://angushardware.com/ InventoryQuery/GetNumberInStock", RequestNamespace:="http://angushardware.com/ InventoryQuery", ResponseNamespace:="http://angushardware.com/InventoryQuery", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _ Public Function GetNumberInStock(ByVal productCode As String) As Integer Dim results( ) As Object = Me.Invoke("GetNumberInStock", New Object( ) {productCode}) Return CType(results(0),Integer) End Function '<remarks/> Public Function BeginGetNumberInStock(ByVal productCode As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult Return Me.BeginInvoke("GetNumberInStock", New Object( ) {productCode}, callback, asyncState) End Function '<remarks/> Public Function EndGetNumberInStock(ByVal asyncResult As System.IAsyncResult) As Integer Dim results( ) As Object = Me.EndInvoke(asyncResult) Return CType(results(0),Integer) End Function End Class
Although SOAP is the default, wsdl.exe
can also
generate client code for HTTP GET and POST services. Use the
command-line argument /protocol:HttpGet
to
generate the HTTP GET version, and
/protocol:HttpPost
to generate the HTTP POST
version.
I’ve used Visual Basic .NET for this example in part to emphasize the fact that a Web Services need not be written in the same language as the server. In reality, the client need not even be a Windows-based computer.
Now that you’ve got the generated
InventoryQuery
proxy class, you can write a
console application to use the proxy to call the web service. Example 10-8 shows one possible implementation in Visual
Basic .NET.
Class InventoryQueryClient Shared Sub Main(byVal args as String( )) Dim query As InventoryQuery = New InventoryQuery( ) System.Console.WriteLine(query.GetNumberInStock(args(0))) End Sub End Class
To compile this code outside of Visual Studio .NET, you’ll need to use the following command line:
vbc.exe /reference:Microsoft.VisualBasic.dll /reference:System.dll / reference:System.Web.Services.dll /reference:System.Xml.dll InventoryQueryClient.vb InventoryQuery.vb
This method of creating Web Services client code hides all the details of the XML and HTTP from you, although it still requires you to implement the web service code on the server side (unless you’re creating a client for some third party’s web service). Obviously, this is a much easier way to create Web Services client code.
Although the
parameter list and behavior are identical, the
InventoryQuery
proxy class generated by
wsdl.exe
is not the same class I wrote in Example 10-2. To clarify the difference, you can specify
the namespace for the generated proxy class by including the
/namespace
argument on the
wsdl.exe
command line. Also remember that the
.asmx
file looks in its .Bin
subdirectory for the assembly containing the
InventoryQuery
class that it uses to serve
requests.
3.145.179.85