View comments | RSS feed

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();

Comments


ritzcoder said on Feb 5, 2003 at 1:39 PM :
Hi. I am trying Flash remoting and would like to know if you can pass constructor info to your class. For example, I have a compiled dll that has a class called EchoClass. The constructor of EchoClass looks like this:

public EchoClass( int i ) {

}

Is it possible to pass i to the constructor using Flash remoting? Thanks very much for any answers.
csabi said on Jun 20, 2003 at 10:38 AM :
The information provided on using assembles is unusable.

Problems with "Returning an ActionScript object" :
public ASObject returnObject()
{
ASObject aso = new ASObject();
aso.ASType = "Calculator";
aso.Add("x", 100);
aso.Add("y", 300);
Flash.Result = aso;
}
This raises an error in the C# compiler as the function does not return anything (though it should by definition).
If you replace the line:

Flash.Result = aso;

with

return aso;

it compiles, but no-one can tell if it will really work.

The echoString example does not work, either. I gave it a try exactly the way it is written here. No success, for each query I get a new line in the logfile:
"No Such Service FlashRemoting.EchoTests.EchoClass with function echoString"

Macromedia... please revise this topic.
csabi said on Jun 23, 2003 at 9:52 AM :
It took some time, but now I have answers to both previous questions.

Issue 1 (the compilation thing):
Replacing the line "Flash.Result = aso;" with "return aso;" actually works fine, I have been able to test it.

Issue 2 (the "No Such Service" problem):
On other Macromedia support topics I have found this is a very frequent error. The workaround is the web.config file. If I use the one provided with the Flash Remoting install, everything just works fine. I started to replace the contents of my original web.config line-by-line with the one provided with MM, and finally found the <authentication> setting is doing the crap. If I set it to "None" and remove the <identity impersonate="true"> settings, the problem goes away.

Please note I cannot tell if this is the correct workaround in 100% of the cases.
agentofchange said on Sep 30, 2003 at 7:49 PM :
The work around didnt work for me :(

Win XP - SP1, Using VS.Net 2003 (framework 1.1)
Lim Hoe Keat said on Nov 13, 2003 at 8:07 PM :
let me point out something here. In this code

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;
}
}
}


THIS CODE DONT WORK!!!!!!!!!

First of all, i dont understand what is this for.

Flash.Result

is Flash an object? The compiler wont be able to find "Flash" at all. Closest i can find is FlashGateway.Flash, which the FlashGateway namespace is not included here, and nothing name Flash exist in the included namespace, which is FlashGateway.IO.

Secondly, the method

public ASObject returnObject()

has a return type of ASObject but the method dont return any thing at all. Irregardless of what the coding do, the method MUST RETURN an object of type ASObject.


Just a couple of question here.

1. Any of u guys know how to write working C# code?
2. Have u guys ever run the so called example here? or just write it for fun?
No screen name said on Nov 26, 2003 at 2:15 PM :
I had a huge headache with a "missing assembly"...

Make sure that you right click on "references" in the explorer window (in VS.net) and browse to "flashgateway.dll".

I know it is something simple and stupid but it took me over a week to figure it out.
pete said on Dec 3, 2003 at 10:06 AM :
1.) No, the class must have a default no-args constructor. You can not call constructors from a NetServices connection in Flash Remoting.

2.) Yes, that example is incorrect. The last line of the returnObject method should be simply:

return aso;

This example must have been accidentally added as an assemby example and placed in the wrong location as it comes from ASPX concepts. The Flash.Result idea comes the ASPX version of a Flash Remoting service, where inline C# can access the Flash page control. Perhaps you're not familiar with ASPX controls?

3.) The example, once corrected, does compile, but perhaps many users are not familiar with C# namespaces. If you use the "using" directive, then at compile time that reference will need to be resolved. If you were compiling C# on the command line, this is simple to do:

csc /r:.\flashgateway.dll /target:library /out:ObjectTests.dll ObjectTests.cs

Or, if you're in VisualStudio, you could also make the flashgateway.dll a reference which the user above points out.
No screen name said on Jan 19, 2004 at 8:13 AM :
Environment: Win XP - SP1, Using VS.Net / VB.NET 2003 (framework 1.1):
Area: FlashRemotingMX / Visual Studio .NET

Problem:
***********
for each Flash-Remoting query to a VB.NET assembly I have created and published according to the Flash Remoting Manual I have got a new line in the flash.log (and NetConnection debugger):
"No Such Service FlashRemoting.EchoTests.EchoClass with function echoString"

Solution (published January 19th, 2004):
******************************************************************
It is not really a problem and it only happens when you are using Visual Studio .NET.
The thing missleading here is, that Macromedia does not mention that, when building the assemblies in VS.NET, VS.NET per default adds the project’s (resp. Class libary) name to the namespace of its classes:
If, for example, you have created the ClassLibrary „myProject“ containing the class-file „myApplication.vb“ with the namespace „myNamespace“ for its class „myClass“ then, according to the Remoting manuals, in the AS code, you would use the fully qualified class name (myNamespace.myClass) in the getService function – which will normally not work when using Visual Studio .NET!
And here is why:
When you right-click the icon of your class library containing your code and you open the class lib's property window you will normally see the default values for the general options set as follows:
-Assemblyname = “myProject”
-RootNameSpace = “myProject” !!! (in German this property is called “Stammnamespace”)

Now you have too options:
Either within AS-Code:
you add the Rootnamespace to the getService-parameter myConnection.getService(myProject.myNamespace.myClass)

OR (not and!!!) within VS.NET:
clear the RootNameSpace-value from the class-library’s property window.

Hope this helps
Kind regards
René
troublesome96 said on Feb 18, 2004 at 7:41 PM :
hi...i've got a problem with this script i got off an e-book...

AS CODE :
-----------------
function Book ( ) {
this.title = "";
this.author = "";
this.price = 0;
}

Book.prototype.bookCost = function (qty) {
return (this.price * qty);
}

Object.registerClass("BookClass", Book);

function GetBook_Result (Results) {
// Display the book's title and the cost of one dozen copies
trace(Results.title);
trace(Results.bookCost(12));
}

#include "NetServices.as"

var myURL = "http://localhost/flashremoting/gateway.aspx";
var servicename = "FRDG.BookAssembly";

NetServices.setDefaultGatewayUrl(myURL);
var connection = NetServices.createGatewayConnection( )
var bookService = connection.getService(servicename, this);

bookService.GetBook( );

-----------------------------------------
C# CODE :
---------------
using System;
using FlashGateway.IO;

namespace FRDG
{
public class BookAssembly
{
public ASObject GetBook ( )
{
ASObject aso = new ASObject( );
aso.ASType = "Book";
aso.Add("title", "FRDG");
aso.Add("author", "Tom Muck");
aso.Add("price", "39.99");
return aso;
}
}
}
----------------------------------------

everything works except the price. for some reason i can't get the price function in the book class to work. it returns undefined all the time. is there like some kind of obvious, trivial mistake i made or something? any help would be appreciated a lot. thanks.
Rooster60602 said on Aug 31, 2004 at 6:53 AM :
How about an example for getting a generic AS object into .NET??? I've tried passing object into method as Hashtable in .NET, and am getting errors...
No screen name said on May 8, 2005 at 10:36 PM :
no such service (SERVICE NAME) with function (FUNCTION NAME)

I get this error but only when i upload the project to my Web server. At the local web server works fine.

I'm using VS2003 and aspx pages with code behind pages

SERVICE NAME : dll's name
FUNCTION NAME : aspx page's name
and i wrote all of my code into page_load event.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRNET6.htm