.NET Invocation guide for Flash Remoting clients Search:
home / articles / NET invocation guide  

INTRODUCTION
WebORB supports a variety of invocation scenarios between Flash Remoting clients and .NET objects. This guide provides a set of instructions with live samples demonstrating Flash Remoting invocations of .NET methods.

CLIENT SETUP

To enable Flash client side binding to a .NET object, Macromedia Flash Remoting components must be installed on the computer where Flash application is developed. Flash compiler integrates the remoting components into the compiled Flash movie, as a result, end user does not have to install anything to run a Flash Remoting-enabled application.

Macromedia Flash Remoting components can be downloaded from: http://www.macromedia.com/software/flashremoting/downloads/components/

SERVER  SETUP

The server side code must be deployed into a WebORB enabled ASP.NET application. Copy application assembly file into the /bin folder of the ASP.NET application and WebORB automatically exposes the classes from the assembly to the Flash Remoting clients.

INVOCATION WALKTHROUGH

  This guide reviews the following invocation scenarios:

Passing primitive argument to a server method

The client invokes the echoInto method in the following class. The method takes a primitive integer argument.
C# code
namespace weborb.tests
{
  public class PrimitiveTest
  {
    public int echoInt( int intArg )
    {
      return intArg;
    }
  }
}

The client side code uses the following code to invoke the .NET method shown above:
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.PrimitiveTest",
                                null, null);

var callObj:PendingCall = serviceProxy.echoInt( 21 );
callObj.responder = new RelayResponder( this, "echoIntResponse", "echoIntFailure" );

function echoIntResponse( resultEventObj:ResultEvent )
{
   // put the result value in the text field
   result.text = resultEventObj.result;
}

Run the client example:  (download the client side code)
 

Passing multiple arguments to a server method

The client invocation will take place on the following class:
C# code
namespace weborb.tests
{
  public class MultipleArgsTest
  {
    public Object[] echoStringBuilderDouble( System.Text.StringBuilder sb, Double d )
    {
      return new Object[] { sb, d };
    }
  }
}

The client side code uses the following script to invoke the .NET method:
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.MultipleArgsTest",
                                 null, null);

var callObj:PendingCall = serviceProxy.echoStringBuilderDouble( "Hello World!", 21 );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
   // put the return value into the text field
   // resultEventObj.result[ 0 ] contains "Hello World"
   // resultEventObj.result[ 1 ] contains 21
   result.text = resultEventObj.result.toString();
}


Run the client example: (download the client side code)
 

Passing date argument to a server method

The client invokes the echoDate method in the following class:
C# code
namespace weborb.tests
{
  public class PrimitiveTest
  {
    public DateTime echoDate( DateTime date )
    {
      return date;
    }
  }
}

The client side code uses the following script to invoke the .NET method:
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.PrimitiveTest",
                                null, null);

var callObj:PendingCall = serviceProxy.echoDate( new Date() );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
    // resultEventObj.result is an instance of ActionScript Date
    // convert it to string and put into the text field
    result.text = resultEventObj.result.toString();
}


Run the client example: (download the client side code)
 

Passing arrays to a server method

The client invokes the echoStringArray method in the following class:
C# code
namespace weborb.tests
{
  public class StringsArrayTest
  {
    public string[] echoStringArray( string[] stringarray )
    {
      return stringarray;
    }
  }
}

The client side code uses the following script to invoke the .NET method:
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.StringsArrayTest",
                                null, null);
var arrayObj = new Array();
arrayObj.push( arrayArg1.text );
arrayObj.push( arrayArg2.text );
arrayObj.push( arrayArg3.text );
var callObj:PendingCall = serviceProxy.echoStringArray( arrayObj );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
   // resultEventObj.result contains an array with the same elements as the original argument
   result.text = resultEventObj.result.toString();
}

Run the client example: (download the client side code)
 

Passing ActionScript objects as complex types to a server method

The client invokes the echoEmployee method in the following class:
C# code
namespace weborb.tests
{
  public class ComplexTypesTest
  {
    public Employee echoEmployee( Employee empl )
    {
      return empl;
    }  
  }

  public class Employee
  {
    private int employeeId;
    private string employeeName;
    private string employeeTitle;
    private string phoneNumber;
  }
}


The client side code uses the following script to invoke the .NET method. The client code creates an ActionScript object with the same fields as the server side class. WebORB automatically adapts the data from the AS object to an instance of the Employee class.
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.ComplexTypesTest",
                                null, null);
var employeeObj = new Object();
employeeObj.employeeId = employeeId.text;
employeeObj.employeeName = employeeName.text;
employeeObj.employeeTitle = employeeTitle.text;
employeeObj.phoneNumber = phoneNumber.text;
var callObj:PendingCall = serviceProxy.echoEmployee( employeeObj );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
  var employeeObj = resultEventObj.result;
  result.text = employeeObj.employeeId + ", " +
                employeeObj.employeeName + ", " +
                employeeObj.employeeTitle + ", " +
                employeeObj.phoneNumber;
}

Run the client example: (download the client side code)
 

Mapping client classes to server types. Using client class as a complex type argument.

In the previous example, the client side method argument is a plain ActionScript object. At times it is necessary to use client side classes instead of generic/anonymous AS objects in the method invocations. What may be even more important is to have the return value from the invocation to be converted to a certain type when the response becomes available. For example, in the example above, the return value is just an untyped ActionScript object that has the same properties as the server-side class. The example below demonstrates how to map a server side type to a client side class.

The client invokes a method in the following class:
C# code
namespace weborb.tests
{
  public class ComplexTypesTest
  {
    public Employee echoEmployee( Employee empl )
    {
      return empl;
    }  
  }

  public class Employee
  {
    private int employeeId;
    private string employeeName;
    private string employeeTitle;
    private string phoneNumber;
  }
}


The client side code uses the following script to invoke the .NET method. Notice the Object.registerClass call. The API call maps an ActionScript class (Employee) to a string identifier of the object (weborb.tests.Employee). By default WebORB uses server-side class name as the identifier. Once the mapping is in place, any instance of the client side Employee class sent as a method argument will be tagged with the mapped identifier. A reverse action occurs with the return values - any instance of weborb.tests.Employee sent by WebORB will be mapped to the client side Employee class. As a result, the code in the gotResponse function can execute a function on the employee object to get a summary of the employee.
ActionScript code
class Employee
{
   var employeeId:String;
   var employeeName:String;
   var employeeTitle:String;
   var phoneNumber:String;

   function getEmployeeSummary()
   {
      return employeeId + ", " +
             employeeName + ", " +
             employeeTitle + ", " +
             phoneNumber;
   }
}

var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.ComplexTypesTest",
                                null, null);

Object.registerClass( "weborb.tests.Employee", Employee );

var employeeObj = new Employee();
employeeObj.employeeId = employeeId.text;
employeeObj.employeeName = employeeName.text;
employeeObj.employeeTitle = employeeTitle.text;
employeeObj.phoneNumber = phoneNumber.text;
var callObj:PendingCall = serviceProxy.echoEmployee( employeeObj );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
var employeeObj = resultEventObj.result;
result.text = employeeObj.getEmployeeSummary();
}


Run the client example: (download the client side code)
 

Invoking server methods with .NET collections as arguments

The client invokes the echoHashtable method in the following class:
C# code
namespace weborb.tests
{
  public class CollectionsTest
  {
    public System.Collections.Hashtable echoHashtable( System.Collections.Hashtable map )
    {
      return map;
    }
  }
}

The client side code uses the following script to invoke the .NET method:
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.CollectionsTest",
                                null, null);
var hashMapObj = new Object();
hashMapObj[ key1.text ] = value1.text;
hashMapObj[ key2.text ] = value2.text;
hashMapObj[ key3.text ] = value3.text;
var callObj:PendingCall = serviceProxy.echoHashtable( hashMapObj );
callObj.responder = new RelayResponder( this, "gotResponse", "gotFailure" );

function gotResponse( resultEventObj:ResultEvent )
{
   // result is an associative array - a data structure closest to hashtable
   // used on the server side. Since the server echoes the argument back, the object
   // is the same as on the input side.
   for( var name in resultEventObj.result )
     result.text += name + " - " + resultEventObj.result[ name ] + "\n";
}

Run the client example: (download the client side code)
 

Storing data in server's session

The client uses the methods in the following class to store and retrieve data from the server. WebORB creates an instance of the SessionData class using the 'session' activation. The activation mode is dictates by additional information provided by the client in the WebORB URL.  As a result of session activation, the object and hence the data stored in the object's field variables stays in the session associated with the client. The client/server session is maintained by a cookie.
C# code
namespace weborb.tests
{
  public class SessionData
  {
    // class stores data in a local private field variable
    private String data;

    //  method to store data
    public void setData( String data )
    {
      this.data = data;   
    }
   
    // method to retrieve data
    public String getData()
    {
      return this.data;
    }
  }
}

The client side code uses the following script to store and retrieve data from the server-side. Notice the "activate" query parameter in the WebORB URL. The "?activate=session" requests that an instance of weborb.tests.SessionData to be created and stored in the session associated with the client.
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx?activate=session";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.SessionData",
                                null, null);

// storing data in session
var callObj:PendingCall = serviceProxy.setData( "Hello World!" );
callObj.responder = new RelayResponder( this, "dataStored", "gotDataStoreFailure" );

function dataStored( resultEventObj:ResultEvent )
{
   var callObj:PendingCall = serviceProxy.getData();
   callObj.responder = new RelayResponder( this, "dataRetrieved", "gotFailure" );
}

function dataRetrieved( resultEventObj:ResultEvent )
{
   // resultEventObj.result contains the data returned from the getData() invocation
   dataFromSession.text = resultEventObj.result;
}


Run the client example: (download the client side code)
 

Handling server side exceptions in the client code

The client invokes a method in the following class. When the y argument is 0, the code throws a 'division by zero' exception.
C# code
namespace weborb.tests
{
  public class ExceptionsTest
  {
    public float divide( int x, int y )
    {
      // division by zero will cause an exception
      return x / y;
    }
  }
}

The client side code uses the following script to execute a server-side method. When the second argument is 0, the method throws an exception. The exception gets handled by a function specified in the responder object.
ActionScript code
var webORBURL = "http://www.themidnightcoders.com/examples/weborb.aspx";
var serviceProxy = new Service( webORBURL, myLogger,
                                "weborb.tests.ExceptionsTest",
                                null, null);

var callObj:PendingCall = serviceProxy.divide( 5, 0 );
callObj.responder = new RelayResponder( this, "divisionResult", "divisionError" );

function divisionResult( resultEventObj:ResultEvent )
{
   result.text = resultEventObj.result.toString();
}

function divisionError( fault:FaultEvent )
{
   // fault.fault.faultstring contains the message of the exception
   // fault.fault.detail contains the server side stacktrace
   result.text = fault.fault.faultstring + "\n" + fault.fault.detail;
}


Run the client example: (download the client side code)
 
Copyright © 2003-2005 Midnight Coders, LLC.  Privacy Policy | Contact Webmaster
Home | Products | Customers | Download | Licensing | Forum | Developers | About