To test a Web service, you first need to create a client application. In this case, you will create a small Console application in Visual Basic .NET. To create a client application, you can use the .NET Framework SDK.
You have already used the WSDL.exe utility to create a server component that was used to create a Web service. Now you will use the same tool to create a client side proxy application that encapsulates the Web service operations.
To create the proxy class, execute the WSDL.exe tool from the command line by using the following syntax:
The preceding command creates a file with the name proxy.vb, which contains the proxy definitions for the client application. The options in the command specify the name of the output file, the language used to generate the proxy class definitions, and the location of the WSDL document for the Web service.
The proxy class contains declarations for all Web methods that the Web service supports.
The proxy class contains two sets of declarations for each Web method. The set of declarations includes synchronous and asynchronous calls for the methods in the Web service.
The definitions in the proxy class are shown in the following code snippet. These declarations refer to the original Web service.
Next, consider the method exposed by the Web service. This method contains the following declarations that allow the Web method to be called synchronously.
The following set of the declarations allows the GetRateByCountry() Web method to be called asynchronously:
After you look at the code for the GetRateByCountry() Web method, you will look at the entire code for the proxy class that the WSDL.exe utility creates, as shown here:
Now create a file, WS1Client.vb, by using a text editor, such as Notepad, and add the following code to it:
You will now create a simple application that contains the Main() method. Copy the entire code from the proxy.vb file to the WS1Client.vb file.
Ensure that the Import declaration statements should be included before the Module1 declaration.
To the WS1Client.vb file, you need to add the code to call the GetCountryData() Web method synchronously. To do this, add the following code to the Main() method of the application:
You can also access GetCountryData() asynchronously by using the Begin() and End() methods in the proxy class, as shown in the following code:
This version of the client application uses the BeginGetRateByCountry() and EndGetRateByCountry() methods to call the GetRateByCountry() operation of the Web service asynchronously. Next, the WaitOne() method delays the execution of the EndGetRateByCountry() method until the Web service call is returned.
Although this is similar to the previous example functionally, in this case, the client application is free to process something else until the Web service call returns.
Compile the synchronous and asynchronous client applications by using the vbc.exe command line or Visual Basic .NET compiler, and verify the functioning of the Web service.
Testing Web Services by Using the Web Service Behavior
Visual Studio .NET makes the task of creating clients that consume Web services fairly simple. However, without Visual Studio .NET, creating clients can be slightly difficult and requires knowledge of handling SOAP messages. To make it easier for a Web developer to consume a Web service, Microsoft provides the Web service behavior Webservice.htc.
You can use the Web service behavior to test a Web service. To use the Web service behavior to access a Web service, perform the following steps:
You first need to attach the Web service behavior to an element in an HTML page. You can attach the Web service behavior to a <div> element by using the following syntax:
Next, you need to initialize the Web service behavior by using the useService() method. The useService() method has the following syntax:
The parameters that are used in the useService() method of the Web service behavior are explained in the following list:
Different Methods of Specifying the URL of a Web Service
For your Web service, you can use the following syntax for the useService() method:
The useService() method takes the URL of the Web service and creates a friendly name, WebService1, which you will use to refer to the Web service. After this, you need to invoke the Web service by using the callService() method. The callService() method initiates the engagement of the Web service behavior with the Web service. The syntax for the callService() method is as shown:
The parameters that are used in the callService() method are explained in the following list:
Tip?/td>The createCallOptions() method contains nine properties. You don't need to set all nine properties, but you do need to set the funcName property to specify the name of the Web method that you want to call. If this property is not set, the Web service will not know the Web method that you want to call.
The syntax for calling a Web method from Webservice1 that you created is shown here:
Apart from the useService()and callService() Web methods, the Web service behavior also supports the createUseOptions() and createCallOptions() methods, as discussed in the following sections.
The createUseOptions() Method
The createUseOptions() method creates a useOptions object that is used to set the authentication persistence property. This property decides whether authentication is required for each method invocation. The syntax for the createUseOptions() method is as follows:
The preceding code creates an ObjReuseAuthentication object of the type use Options and has a property reUseConnection. This property is useful when the Web site that hosts the service uses SSL. In addition, by using this property, you can specify whether the Web service client needs to log in for each method invocation. The code for the createUseOptions() method for WebService1 is as follows:
The createCallOptions() method creates an object of the type options for the Web service behavior. The syntax for the method is shown here:
This method can initialize callObject, which can be passed as a parameter to the callService() method. The following is a sample snippet of the callService() method:
As you can see, the createCallOptions() method returns a call options object.
The Result Object
The Web service behavior uses the result object to return a value from the Web service. The result object uses the onresult and onServiceAvailable event handlers, as discussed in the following sections.
The on result Event Handler
You can use the onresult event handler to access this data. The onresult event handler is accessible only to the HTML element that is attached to the Web service behavior.
In the preceding syntax, you specified that the ProcessResult() method will be called when the onresult event is fired. You can then use the ProcessResult() method to retrieve the value returned by the Web service, if any, or to collect the information about an error that might have generated. The code for the ProcessResult() method is shown here:
The onServiceAvailable Event Handler
The onServiceAvailable() event handler is fired after the behavior has successfully obtained the WSDL from a Web service. This event has the following properties:
The Web service behavior makes it easy to access Web services from Web pages. However, the behavior has certain limitations, as discussed in the following list:
Now that you have briefly learned about the Web service behavior, you will learn to create an HTML page that you can use to connect to Webservice1 and retrieve data from it.
The preceding code creates an HTML page that contains a Button control.
The Web service behavior is bound to the <div> element with an ID of service1. The body element has an onLoad event handler that calls the useService() method to initialize the location of the Web service with which the behavior will interact.
When the button is clicked, the callWS() method is called, which in turn calls a Web service method using the callService() method. Then the method calls the getRateByCountry() method with the name of a country passed as a parameter.
After the method call returns, the control is passed to the ProcessMessage() method, which handles the onResult event. The method returns a returned value or an error message, whichever applies.
You can save and open the HTML file in Internet Explorer 5.0 or later to test the working of the Web service.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
Basics Of Xml
Basics Of Web Services
Introduction To Soap
Introduction To Uddi
Introduction To Wsdl
Creating A Web Service Using The Microsoft Soap Toolkit
Building Web Applications On The .net Platform
Creating An Asp.net Web Service
Creating A Web Service From An Interface
Introduction To The Atl Server
Creating A Web Service Using The Atl Server Library
Design And Creation Of The Knowledge Share Web Service
Introduction To Java Xml Technologies
Developing Java Web Services
Design And Creation Of A Web Service Using The Ibm Toolkit
Introduction To Mobile Applications
Creating A Mobile Application That Consumes A Web Service
Web Services Development With Jdeveloper
Creating Web Services Using Perl
Integration Of Xml Web Services With The Office Xp And Sql
Server 2000 Toolkits
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.