Accessing a WCF – SOAP service from Flex

Posted on Updated on

Flex development for a .NET guy

I’ve been working on Flex for last couple weeks using Flash Builder 4.7. I touched the second Adobe product in my life first is Dreamweaver, not Photoshop. I know that’s quite surprising but the truth is I haven’t used Photoshop yet and I don’t even know how to use it.

Leaving my history aside; I started Flex and soon I had to create some complex controls for the project. I did them and now I think I do fairly good. I write this post in order to help the newbies like me who jump into Flex development without any clue and for my personal reference.

As a developer you can put some basic controls and do simple development. You can find plenty of beginners tutorials for Flex. This post  explains how to make a web service call from Flex and access data.

Flex make it very simple to connect to a SOAP based services.

   1: <?xml version="1.0" encoding="utf-8"?>

   2: <s:Application     xmlns:fx="http://ns.adobe.com/mxml/2009" 

   3:                    xmlns:mx="library://ns.adobe.com/flex/mx" 

   4:                    xmlns:s="library://ns.adobe.com/flex/spark"

   5:                    creationComplete="application_creationCompleteHandler(event)">

   6:     <fx:Script>

   7:         <![CDATA[

   8:             

   9:             private var serverServiceUrl:String = "http://localhost/yourservice.svc?wsdl";

  10:             

  11:             [Bindable]

  12:             private var prodSAH:Object;

  13:             

  14:             protected function application_creationCompleteHandler(event:FlexEvent):void

  15:             {

  16:                 productionService.GetProductionData("SAH","","","Month To Date");

  17:             }    

  18:             

  19:             

  20:             protected function productionService_resultHandler(event:ResultEvent):void

  21:             {

  22:                 prodSAH = event.result as Object;

  23:  

  24:                 Alert.show(prodSAH.CompanyName);

  25:             }

  26:             

  27:             

  28:                    

  29:         ]]>

  30:     </fx:Script>

  31:     

  32:         

  33:     <fx:Declarations> 

  34:         

  35:         <s:WebService id="productionService" wsdl="{serverServiceUrl}" result="productionService_resultHandler(event)">

  36:         </s:WebService>

  37:             

  38:     </fx:Declarations>

  39:     

  40:     

  41:     

  42:     

In the code above [Bindable] attribute makes the variables responsively coupled with the controls, if you’re from Silverlight background you can understand this as a INotifyPropertyChanged interface implemented variables. Any change to those variables would reflect in the UI.

“Declarations” section contains the declarations of the MXML application, web service reference sits here. GetProductionData is one OperationContract of the WCF service and it returns a Production object as SOAP message. Production object contains several properties, here I have shown how to access the properties. It is simple, just put a dot(.) and type the property name (in this case CompanyName). But when you put the dot don’t expect Intellisense to be popped out and give you suggestions. It will never happen, so be careful about the spelling.

If you’re getting a collection from the service you can simply load it into a ArrayCollection in Flex.

I haven’t included any UI controls in this sample, but they’re straight forward as XAML markup (probably MXML came first).

Flash Builder IDE

This section discusses about the Flash Builder as an IDE.

In my Boolean world if some one asks me how do I rate Flash Builder as an IDE either god or bad it’s definitely bad.

I use Flash Builder 4.7. As an IDE it has plenty of glitches and personally for me as a person who spend entire work time with VS this is a direct kick on my midriff as a new developer for Flex. The IDE continuously eats up your heap. There’s an option that you can enable to view the heap size of the IDE. In my 4GB work station my OS allocates 740 ~ 760 Mb for Flash Builder. But after few builds during development the entire heap is full and IDE stops responding and goes dead slow. I often have to restart the IDE in order to work in my pace. I checked with other developers as well, same story.

And mainly there’s a disastrous problem there’s no design view. Believe it of course no design view. I got to know that they removed the design view from this version onwards. 

When you create a project Flash Builder creates a default MXML application in the name of the project. One project can contain multiple MXML applications. But if you delete the MXML file in the name of the project you cannot export any other MXML files. This is really a stupid and senseless bug.

As a VS lazy guy Intellisense is the god of development, in Flash Builder it’s there for the sake. Almost all the time I have to bring it up after putting a dot using Ctrl + Space. There’s a hell lots of improvements needed for Flash Builder as an IDE.

Advertisements