Invocation of a .NET object using the <mx:RemoteObject> MXML declaration can be done in just a few simple steps. This article will guide through the setup and onfiguration:
- Download the latest version of WebORB for .NET
- Deploy your .NET assembly with the class you will be invoking from Flex into WebORB. This can be done simply by copying the DLL into the /bin folder.
- Open WEB-INF\flex\remoting-config.xml in a text editor and add a destination declaring your .NET type. Use the following format:
<destination id="destination-name">
<properties>
<source>Your.Net.ClassName</source>
</properties>
</destination>
where,
- destination-name is an id assigned to your .NET class. The id will be used by Flex client when invoking server-side methods
- Your.Net.ClassName full name of a .NET type exposed as a service to Flex clients
- Create a Flex Builder project as it is described in this article.
- Add the following remote object declaration to your MXML's application markup:
<mx:RemoteObject id="remote-object-id" destination="destination-name"
showbusycursor="true" fault="faultHandler(event)">
<mx:method name="methodToInvoke" result="successHandler(event)">
</mx:RemoteObject>
where,
- destination-name must be the same literal as destination id set in the server-side config
- remote-object-id is an id used in the MXML application to refer to a remote object
- methodToInvoke is the method name available in the remote destination that your remote object can invoke
- successHandler and faultHandler are function references your MXML application will invoke upon
successful or unsuccessful method invocation
- Suppose the following .NET class needs to be consumed by a Flex client:
namespace weborb.tests
{
public class HelloWorld
{
public string sayHello()
{
return "Hey Flex";
}
}
}
then the destination declaration may look as the following:
<destination id="helloWorld">
<properties>
<source>weborb.tests.HelloWorld</source>
</properties>
</destination>The <RemoteObject> declaration would have the following contents:
<mx:RemoteObject id="helloWorldService" destination="helloWorld"
showbusycursor="true" fault="faultHandler(event)">
<mx:method name="sayHello"result="gotHello(event)">
</mx:RemoteObject>The callback handler functions (gotHello and faultHandler) could be declared as:
private
function gotHello(event:ResultEvent):void
{
Alert.show("Server said: " + event.result, "Success");
}
private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultstring, "Error");
}
- Now anywhere in your MXML application you can invoke sayHello using the following line of code:
helloWorldService.sayHello();