Calling ASP.NET assemblies from Flash

Using Flash Remoting MX, you can invoke .NET assembly files (*.dll) from Flash. In your ActionScript code, you use the fully qualified assembly or class file name in the getService function, and for the service function name, you use an assembly or class method name. On the server, you must place your DLL and class files in the local assembly cache.

Calling assemblies from Flash

In the class file, you reference the Flash Remoting assembly namespace FlashGateway.IO with the using directive, as the following C# example shows:

using System;
using FlashGateway.IO;
namespace FlashRemoting.EchoTests
{
  public class EchoClass
  {
    public EchoClass()
    {
      ///Public constructor... initialize any member fields here if need be.
    }
    public string echoString(string s)
    {
      return s;
    }
  }
}

In the ActionScript, you use the namespace and public class name defined in the class file, as the following example shows:

#include "NetServices.as"
#include "NetDebug.as" 
if (inited == null)
{
  inited = true;
  NetServices.setDefaultGatewayUrl("http://localhost/myASPApp/default.aspx");
  gatewayConnection = NetServices.createGatewayConnection();
  classService = gatewayConnection.getService("FlashRemoting.EchoTests.EchoClass");
  classService.echoString(input.text);
}
function echoString_Result(result)
{
  stringDisplay.text = result;
}
function echoString_Status(result)
{
  stringDisplay.text = error.description;
}

In the code, you use the fully qualified class name (FlashRemoting.ClassService.EchoClass) in the getService function. To call an assembly method, you use the class method name (echoString) as defined in the class file.

Returning an ActionScript object from an assembly

You can use the ASObject class of the FlashGateway.IO namespace to create and populate ActionScript objects in ASP.NET and return the object to Flash. By passing ActionScript objects back and forth between the remote service and the Flash application, you can describe the data being passed with the ASType property of the ASObject class.

Creating an assembly that returns an ActionScript object

In the assembly, you create an instance of the ASObject class of the FlashGateway.IO namespace and return it to Flash. The ASType property lets you assign a name to the object for identification in Flash. To add values to the object, you use the Add method common to instances of the .NET Collections class, as the following C# example shows:

using System;
using FlashGateway.IO;
namespace FlashRemoting.ObjectTests
{
  public class ObjectClass
  {
    public ObjectClass()
    {
      ///Public constructor... initialize any member fields here if need be.
    }
    public ASObject returnObject()
    {
      ASObject aso = new ASObject();
      aso.ASType = "Calculator";
      aso.Add("x", 100);
      aso.Add("y", 300);
      Flash.Result = aso;
    }
  }
}

In the code, an instance of the ASObject object, named aso, is created, and the ASType property is used to identify the object as Calculator. The Add method inserts key-value pairs into the object. Finally, the aso object is returned to Flash using the Flash.Result variable.

Handling the ActionScript object in Flash

The following ActionScript handles the ActionScript object returned by the assembly:

#include "NetServices.as"
#include "NetDebug.as" 
 
if (inited == null)
{ 
  inited = true;
  NetServices.setDefaultGatewayUrl("http://localhost/myASPApp/gateway.aspx");
  gatewayConnection = NetServices.createGatewayConnection();
  dataService = gatewayConnection.getService("FlashRemoting.ObjectTests.ObjectClass", this);
}
 
//Ask the server for some raw data...
dataService.returnObject();

//Provide a callback for getNumbers() when the data returns from the server
function returnObject_Result ( result )
{
 /*
 Because we expect the result to represent the state of a Calculator instance 
 back from the server, we can assume the result will have an add() method.
 */
 resultBox.text = result.add();
}

/*
Rich Client Business Logic
*/
calc = function()
{
  this.x = 0;
  this.y = 0;
}
calc.prototype.subtract = function()
{
  return this.x - this.y;
}
calc.prototype.add = function()
{
  return this.x + this.y;
}
Object.registerClass("Calculator", calc);

stop();