WCF and Flex provide numerous benefits for developing server-side functionality in .NET and implementing rich client applications with Flex/ActionScript. The question of integration between these two technologies comes up quite frequently as developers and business want to leverage the best the frameworks have to offer.
There are at least two approaches for solving the problem. One is to use HTTP/SOAP binding in WCF to expose WCF services as Web Services. Flex client can connect to WCF using the WebService API. Another approach is to use AMF/Remoting on the Flex side and leverage the power of a remoting gateway on the server to communicate with WCF. The recipe review the latter approach using WebORB for .NET as the remoting gateway. WebORB is available as a free product in the form of Community Edition or commercially as Enterprise Edition. All the features described in the recipe are available at no cost in the Community Edition of the product.
using System;
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add( double n1, double n2 );
[OperationContract]
double Subtract( double n1, double n2 );
[OperationContract]
double Multiply( double n1, double n2 );
[OperationContract]
double Divide( double n1, double n2 );
}
public class CalculatorService : ICalculator
{
public double Add( double n1, double n2 )
{
return n1 + n2;
}
public double Subtract( double n1, double n2 )
{
return n1 - n2;
}
public double Multiply( double n1, double n2 )
{
return n1 * n2;
}
public double Divide( double n1, double n2 )
{
return n1 / n2;
}
}
}
<?xml version="1.0" encoding="utf-8"
?>
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.ServiceModel.Samples.CalculatorService">
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>
<?xml version="1.0"
encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
[Bindable]
private var ops:Array = ["+", "-", "*", "/" ];
private var wcfProxy:RemoteObject;
private function init():void
{
wcfProxy = new RemoteObject( "GenericDestination" );
wcfProxy.source =
"Microsoft.ServiceModel.Samples.CalculatorService";
wcfProxy.addEventListener( ResultEvent.RESULT, gotResult );
wcfProxy.addEventListener( FaultEvent.FAULT, gotError );
}
private function calculate():void
{
if( opList.selectedLabel == "+" )
wcfProxy.Add( arg1.text as Number, arg2.text as Number );
else if( opList.selectedLabel == "-" )
wcfProxy.Subtract( arg1.text as Number, arg2.text as
Number );
else if( opList.selectedLabel == "*" )
wcfProxy.Multiply( arg1.text as Number, arg2.text as
Number );
else
wcfProxy.Divide( arg1.text as Number, arg2.text as Number
);
}
private function gotResult( event:ResultEvent ):void
{
result.text = event.result.toString();
}
private function gotError( event:FaultEvent ):void
{
Alert.show( "Server reported an error - " +
event.fault.faultDetail );
}
]]>
</fx:Script>
<s:Panel y="10" height="258" width="405" x="10" title="WCF
Calculator">
<mx:Form left="20" right="20" top="20">
<mx:FormItem label="Argument 1">
<s:TextInput id="arg1" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Operator">
<mx:ComboBox id="opList" editable="false" width="100%"
dataProvider="{ops}" />
</mx:FormItem>
<mx:FormItem label="Argument 2">
<s:TextInput id="arg2" width="100%"/>
</mx:FormItem>
</mx:Form>
<s:Button x="20" y="149" label="Calculate"
click="calculate()"/>
<s:Label x="21" y="183" text="Result:"/>
<s:Label x="69" y="178" id="result" />
</s:Panel>
</s:Application>
+