Blog to discuss Midnight Coders products features, ideas and trends in development of Rich Internet Applications

Thursday, June 22, 2006

Invoking .NET objects with mx:RemoteObject

If you ever wondered how to invoke a .NET object using the mx:RemoteObject declaration, it can be done in just a few simple steps. This mini tutorial will guide through the setup and configuration:
  1. Download the latest beta of WebORB 2.1 (make sure not to downgrade to the Standard Edition - it does not have Flex integration)
  2. 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.
  3. Open WEB-INF\flex\flex-remoting-service.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

  4. 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
  5. 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

  6. 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");
    }

  7. Now anywhere in your MXML application you can invoke sayHello using the following line of code:

    helloWorldService.sayHello();

0 Comments:

Post a Comment

<< Home