Original URL: https://www.theregister.com/2008/07/17/flex_gets_soapy/

Consume .NET services without Silverlight

Adobe Flex comes through

By Tim Anderson

Posted in Software, 17th July 2008 16:11 GMT

Web bling tone Can Adobe Systems' Flex access Microsoft .NET web services? Adobe tends to promote its own ColdFusion or LiveCycle Data Services for use with Flex, but it also has support for SOAP 1.1.

When Microsoft released Silverlight 2 second beta last month, I tested it by building a simple Create, Retrieve, Update, Delete (CRUD) database application, following all the defaults: SQL Server, ASP.NET, Windows Communication Foundation (WCF) web service, and of course Visual Studio 2008 to bring it all together. It wasn't entirely painless, but the problems were largely in getting Microsoft's beta tools to install correctly; in the end it worked well.

I could not resist trying Flex with the same web service. Since I had used the BasicHttpBinding for the WCF service, it should be compatible. How easy would it be? It is a key point for developers on Microsoft's platform who may be eyeing Flex or AIR with interest.

I installed my web service into IIS 7.0 and tested it. It is a simple affair, designed to be the back end for a rudimentary guestbook. There is a GuestItem class, which represents an entry in the book, and a web service with three methods: GetGuestEntries, SaveGuestItem and DeleteGuestItem. I fired up FlexBuilder, created a new Flex web application project, and added a DataGrid and a few other controls that I hoped to populate with my data. Then I chose Import Web Service from the Data menu.

Import WSDL screen grab

Microsoft's web service is sucked into FlexBuilder

FlexBuilder proceeded to generate nearly forty ActionScript files in order to wrap the service. Still, I was in luck: the import worked. Adobe is actually using a modified version of the excellent Apache Axis2, which is encouraging.

The next question was how to talk to the service. I opened GuestBook.as, and was delighted to find comments with code samples, even though they were not quite complete. The basic pattern is this:

1. Create an instance of the web service wrapper class

2. Add a listener that corresponds to the method you will call. The web service is called asynchronously, and the function you supply for the listener will be called when the result is received

3. Add a listener for SOAP faults. This is a kind of exception handler

4. Call the service method

Fortunately these steps only take a few lines of code. Here is the listener function, which binds the list of entries to the DataGrid:


  public function getGuestItemsResult(e:GetGuestItemsResultEvent):void {
        grdGuestBook.dataProvider = e.result;
  }

Here is the code which calls the web service:


   public function refreshGuestList():void {
   var guestService:GuestBook= new GuestBook();
   guestService.addgetGuestItemsEventListener(getGuestItemsResult);
   guestService.addGuestBookFaultEventListener(getFaultResult);
   guestService.getGuestItems();        
   }


Incidentally, the C# method that implements the service looks like this:


[OperationContract]
public List<GuestItem> GetGuestItems()

The ActionScript wrapper represents this as a custom class, ArrayOfGuestItem, which extends ArrayCollection, and this binds nicely to a DataGrid. I just needed to set the dataField attribute of each column in the grid to match the properties of a GuestItem.

Unfortunately my application failed with a fault message: "Security error accessing url". Running the application in debug mode got me a more explicit message: "Denied due to lack of policy file permissions". Although I was using a local web server, Flex considered it to be a different domain. I had to add a crossdomain.xml file to the web application explicitly giving permission for cross-domain use. Silverlight is equally cautious, but currently gives a less helpful 404 error if it cannot find a suitable policy file.

After that, everything worked. A few lines of code later, and my Flex app could also create and delete rows in SQL Server, via same WCF web service.

Building a CRUD app for Flex

Flex client, meet .NET service

In its online help, Adobe remarks that: "SOAP is often very verbose and heavy across the wire, which can result in higher client-side memory requirements and processing time."

It is likely true, and it would be interesting to test. Another concern is that large amounts of wrapper code can make debugging difficult, in the event of subtle SOAP compatibility issues. On the other hand, the ability to create Flex clients that seamlessly interoperate with existing .NET web services is a real advantage. Further, provided you don't peer too closely at the innards, Visual Studio does a good job of simplifying web service implementation.

What about Silverlight versus Flex? Silverlight 2 is in beta, and still lacks a visual designer unless you wheel out Expression Blend, whereas FlexBuilder has this built-in.

On the Silverlight side, using a WCF web service is a little easier, as you would expect from a single-vendor stack, and I suspect its performance is better. That said, and leaving aside the large amount of wrapper code, which Flex generated, the code in my simple client ended up similar in both.

Given that I was initially skeptical whether this would work at all, it is a good result for Adobe's client; it also suggests that for many projects either one will do.®