How to test a Web Service XML

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.

Note?/td>
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.

Tip?/td>
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.

Note?/td>
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:

  1. Attach the Web service behavior to any element in a Web page.
  2. Specify the URL of the Web page by using the useService() method of the Web service behavior.
  3. Invoke a Web method by using the callService() method of the Web service behavior.

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:

  • sElementID. This is a required parameter that specifies the ID of the element to which you attach the Web service behavior.
  • sWebServiceURL. This is a required parameter that specifies the URL of the Web service. You can specify the URL of the Web service in four ways, as shown in Table.
  • Different Methods of Specifying the URL of a Web Service

    Different Methods of Specifying the URL of a Web Service

  • sFriendlyName. This is a string parameter that represents a friendly name for the Web service URL. It is a required parameter.
  • oUseOptions. This is an instance of the useOptions object. It contains a single property, reuse Connection, which specifies the connection information required by a Web service that uses Secure Sockets Layer (SSL) authentication.

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:

  • iCallID. This is the return ID of the service call.
  • sElementID. This is the ID of the element in the Web page that is attached to the Web services behavior.
  • Tip?/td>With asynchronous calls, it is essential to match the iCallID
  • parameter with the ID that is returned as a property of the result
  • object. The result object cannot be associated with the service call
  • until the two IDs match.
  • sFriendlyName. This is the friendly name, as specified in the useService() method, that is associated with a Web service.
  • CallHandler. This is an optional parameter used as a callback handler function for processing the result object.
  • funcOrObj. This is a required parameter that specifies either the Web method that you want to call or the call options object. You can set this object with the createCallOptions() method.

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.

  • oParam. This is a list of required parameters that the Web method expects. The parameters are separated by commas.

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:

ThecreateCallOptions() Method

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:

  • ServiceAvailable. This is a Boolean property that indicates whether the behavior was successful in retrieving the WSDL from the Web service.
  • ServiceURL. This is a string value that contains the URL of the Web service, which is connected to the behavior.
  • username. This property contains the string that you passed as the friendly name to the useService() method.
  • WSDL. This property contains the WSDL for the Web service, which is connected to the behavior.
  • 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:

  • You can use the behavior to access only those Web services that are in the same domain as the Web page that contains it.
  • Although the behavior supports all .NET data types, it does not support the complex types, such as datasets, recordsets, and other user-defined types.

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.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics