This article is more than 1 year old

Consume .NET services without Silverlight

Adobe Flex comes through

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.

More about

TIP US OFF

Send us news


Other stories you might like