Invoking .NET objects with mx:RemoteObject
If you ever wondered how to invoke a .NET object using the mx:RemoteObject
- Download the latest beta of WebORB 2.1 (make sure not to downgrade to the Standard Edition - it does not have Flex integration)
- 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\flex-remoting-service.xml in a text editor and add a destination declaring your .NET type. Use the following format:
where,
<destination id="destination-name">
<properties>
<source>Your.Net.ClassName</source>
</properties>
</destination>- 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 the "GETTING STARTED" section in the following article: http://www.themidnightcoders.com/articles/flextodotnet.htm
- Add the following remote object declaration to your MXML's application markup:
<mx:RemoteObject id="remote-object-id"
where,
destination="destination-name"
showbusycursor="true"
fault="faultHandler(event)">
<mx:method name="methodToInvoke"
result="successHandler(event)">
</mx:RemoteObject>- 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:
then the destination declaration may look as the following:
namespace weborb.tests
{
public class HelloWorld
{
public string sayHello()
{
return "Hey Flex";
}
}
}<destination id="helloWorld">
The <RemoteObject> declaration would have the following contents:
<properties>
<source>weborb.tests.HelloWorld</source>
</properties>
</destination><mx:RemoteObject id="helloWorldService"
The callback handler functions (gotHello and faultHandler) could be declared as:
destination="helloWorld"
showbusycursor="true"
fault="faultHandler(event)">
<mx:method name="sayHello"
result="gotHello(event)">
</mx:RemoteObject>
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();







0 Comments:
Post a Comment
<< Home