Consuming a Web Service in Notepad

336 In the Address field of the Add Web Reference dialog box, enter the URI of a web-service description document, and click the Add Reference button. When adding references to web services hosted by ASP.NET, the web-service description document is obtained by appending ?wsdl to the path of the service itself. For example: http:www.company.commyWebService.asmx?wsdl or http:localhostWebServicesHello-WebService.asmx?wsdl. Visual Studio .NET reads the web-service description document and builds a corresponding proxy class that knows how to access the described web service. The proxy class is local and exposes the same functionality as the web service. To call the web service from within the project, instantiate the proxy class and call its methods. For example, the following code instantiates the HelloWebService class and calls its SayHello method in response to a button click: Assumes that this code is part of a form having a button named btnOk_Click, a text box named txtName, and a text box named txtResult. Private Sub btnOk_Click _ ByVal sender As System.Object, _ ByVal e As System.EventArgs _ Handles btnOk.Click If txtName.Text Then Dim sName As String = txtName.Text Dim oWS As New localhost.HelloWebService Dim sMsg As String = oWS.SayHellosName oWS.Dispose txtResult.Text = sMsg End If End Sub Note that the web-server machine name is used as the namespace for the web-service class. Thus, for a class called HelloWebService exposed by the same machine as the caller, the namespace is localhost, and the fully qualified name of the class is localhost.HelloWebService. For machine names containing dots, the order of the parts is reversed. Thus, if the HelloWebService service were exposed by a machine named webservices.somecompany.com, the HelloWebService class namespace would be com.somecompany.webservices, and the fully qualified class name of the class would be com.somecompany.webservices.HelloWebService. Internally, the proxy class packages up method calls into SOAP wrappers and forwards them to the web service. This process is transparent to the client of the proxy class.

7.4.2 Consuming a Web Service in Notepad

To use a web service from a project that is not developed within the Visual Studio .NET IDE, use the command-line wsdl.exe tool to create a proxy class that wraps the web service. For example: wsdl http:localhostWebServicesHelloWebService.asmx?wsdl language:vb The wsdl.exe tool takes as an argument the URL of a web-service description document. As explained in the preceding discussion of Visual Studio .NET, when consuming services hosted by ASP.NET, the web-service description document is obtained by appending ?wsdl to the path of the web service, as was done here. The output of the wsdl.exe tool is a source-code file containing the definition of a proxy class that knows how to access the described web service. The wsdl.exe tools language switch controls whether the source is written in Visual Basic .NET or C. The name of the class in the generated source code is equal to the name of the service, as given in the web-service description document. Ex am ple 7- 2 shows the output of the wsdl.exe tool when run on the HelloWebService service from 337 Ex am ple 7- 1 . Note that several of the lines have been reformatted to make them fit in this book. Other than that, the code is unchanged. Example 7-2. Sample output of the wsdl.exe tool -------------------------------------------------------------------------- autogenerated This code was generated by a tool. Runtime Version: 1.0.2914.16 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.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.2914.16. System.Web.Services.WebServiceBindingAttribute _ Name:=HelloWebServiceSoap, _ [Namespace]:=http:yourcompany.com _ Public Class HelloWebService Inherits System.Web.Services.Protocols.SoapHttpClientProtocol System.Diagnostics.DebuggerStepThroughAttribute _ Public Sub New MyBase.New Me.Url = http:localhostWebServicesHelloWebService.asmx End Sub System.Diagnostics.DebuggerStepThroughAttribute , _ System.Web.Services.Protocols.SoapDocumentMethodAttribute _ http:yourcompany.comSayHello, _ RequestNamespace:=http:yourcompany.com, _ ResponseNamespace:=http:yourcompany.com, _ Use:=System.Web.Services.Description.SoapBindingUse.Literal, _ ParameterStyle:= _ System.Web.Services.Protocols.SoapParameterStyle.Wrapped _ Public Function SayHelloByVal Name As String As String Dim results As Object = Me.InvokeSayHello, New Object {Name} Return CTyperesults0,String End Function System.Diagnostics.DebuggerStepThroughAttribute _ Public Function BeginSayHello _ ByVal Name As String, _ ByVal callback As System.AsyncCallback, _ ByVal asyncState As Object _ As System.IAsyncResult Return Me.BeginInvokeSayHello, New Object {Name}, callback, _ 338 asyncState End Function System.Diagnostics.DebuggerStepThroughAttribute _ Public Function EndSayHello _ ByVal asyncResult As System.IAsyncResult _ As String Dim results As Object = Me.EndInvokeasyncResult Return CTyperesults0,String End Function End Class After creating the proxy class, write client code to make use of it. Using the proxy class is just like using any other .NET class. The proxy class hides method calls being forwarded over the Web. Ex am ple 7- 3 shows code that uses the proxy class from Ex am ple 7- 2 . Example 7-3. Client code for use with Ex a m ple 7 - 2 Imports System Public Module SomeApplication Public Sub Main Dim myService As New HelloWebService Console.WriteLinemyService.SayHelloAnnemarie End Sub End Module Note that the client code simply instantiates the proxy class and calls its SayHello method. Lastly, compile the proxy class and the client code together. Heres an example of compiling from the command line note that this should be entered as a single command and is shown on two lines here only for printing in this book: vbc SomeApplication.vb HelloWebService.vb reference:System.Web.Services.dll,System.Xml.dll,System.dll The three assemblies referenced in this command are required by the proxy class.

7.4.3 Synchronous Versus Asynchronous Calls