by Adobe
Adobe
Created
14 July 2008
Prerequisites
Working knowledge of SOAP messaging and ActionScript 3.0.
 
User level
Intermediate
Required products
Flex Builder 3 (Download trial)
Sample files
hellows.zip (24 KB)
Web service standards are an important cornerstone of enterprise SOA. While the Flex platform has always had native support for web services, Flex Builder 3 has taken it one step further with a new feature, Web Service Introspection.
 
Using a modified version of Apache Axis framework, the new Web Service Introspection wizard generates proxy classes that take care of web service data transfer and event plumbing. With this new tool, an application developer can build a web-service–enabled Flex client in minutes!
 
This article shows how to use the Web Service Introspection Tool, and explains the code it generates for a sample application.
 

Using the Web Service Introspect wizard

After opening a Flex project in Flex Builder, choose Data > Import Web Service. The wizard will ask for the WSDL URI and a path to save the generated code (see Figure 1). The Web Services Description Language (WSDL) document contains information that a client needs to use the web service.
 
Figure 1. The Web Service Introspection wizard.
Figure 1. The Web Service Introspection wizard.
After inspecting the WSDL file, the wizard presents code generation options (see Figure 2). You can select which web service operations to target, and specify the fully qualified name for the ActionScript class.
 
Figure 2. Code generation options.
Figure 2. Code generation options.
You can choose Data > Manage Web Services to access the Manage Web Services dialog box, which is useful for updating and deleting WSDL information, as well as managing multiple web services associated with a project (see Figure 3).
 
Figure 3. The Manage Web Services dialog box.
Figure 3. The Manage Web Services dialog box.

 
Understanding the generated Code

Depending on the complexity of the WSDL, there can be an almost overwhelming number of classes generated. The actual mechanics of the generated classes is quite simple, however.
 
First, look at the class in the <Service Name>.as file (in the example shown in Figure 2, this would be USWeather.as). It's the proxy class that you use to drive the web service calls. Note that the WebService class is no longer used in Flex. All the service calls are made through <Service Name>.as.
 
In addition, all the web service members are listed in <Service Name>.as as public functions. There are two ways you can invoke a service call:
 
  • Attach an event handler to a service call. If you come from a Java/.NET background, this is would be the most intuitive approach. After instantiating the main service class, attach an event handler to the service call and proceed to invoke the service call. For example:
private var weather:USWeather = new USWeather(); private function getMyWeather1():void { // Attach the event handler weather.addgetWeatherReportEventListener(handleWeather); // Invoke the service call weather.getWeatherReport("37217"); } private function handleWeather(event:GetWeatherReportResultEvent):void { trace(event.result); }
  • Bind to the last result. This approach leverages the Flex data binding feature and simplifies your code. To make a service call, pass the parameters via <method>_Request_var and invoke <method>_send. The return data will be stored in <method>_lastResult. For example:
[Bindable] private var weather:USWeather = new USWeather(); private function getMyWeather2():void { // Set the input parameter var req:GetWeatherReport_request = new GetWeatherReport_request(); req.ZipCode = "37217"; weather.getWeatherReport_request_var = req; // Invoke the method weather.getWeatherReport_send(); }
All data exchange is done via strongly typed data structures. For example, if you were making a call to getCustomer, which returns a CutomerInfo object, the object type would be defined by the CutomerInfoType class.
 
Some web service endpoints require basic authentication. To set the credentials, use the setCredentials(), setRemoteCredentials(), and logout()functions from the base class, Base<Service Name>.as.
 

Where to go from here

The new Web Service Introspection wizard has made developing SOAP clients effortless in Flex. The strongly typed message passing not only gives you a water-tight communication pipe, but it also makes coding much easier in the IDE.
 
One drawback of using web services is the communication overhead. If your Flex application handles large amount of data, consider using the BlazeDS Remote Object Service instead.