Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Consuming .NET with Flash

Ever since joining Fuel Industries, I’ve had a new found respect for Flash development. Flash isn’t well suited for every types of development, for example I wouldn’t want to see google.com fully implemented in flash! When it comes to adver-gaming, which Fuel Industries is a leader of, Flash rules supreme (although more and more of our development is moving towards Virtools). You simply can’t build the same type of web-based immersion/branding in HTML/ASPX (even with Atlas/Ajax). We even build traditional web-based intranet application using Flash frontends. The end result is always something that impresses me a lot.that seems much more desktop-based than web-based.

Flash is just a presentation layer however, so how can it consume a business layer? The best solution is to use Macromedia’s Flash Remoting Technology, which uses a proprietary binary stream (AMF) to communicate with server-side code. Anyone familiar with AJAX should have a good feel for Flash Remoting. The main difference being that you aren’t using JavaScrit or XML – but rather ActionScript and AMF.

The problem with Macromedia’s Flash Remoting technology is the cost. It’s about $1000USD – which isn’t cheap. (the other problem is the “crapstically” written documentation, which I’ve railed against before). In response to the high price, a group of PHP developers created the highly popular AMFPHP – an open source alternative for PHP. This led to the development of OpenAMF – an open source alternative to Java. The first thing I was tasked with at Fuel was to create an open source .NET alternative – AMF.NET. 

(there were some .NET alternatives already available, namely Fluorine, but I had some strong reservations against it).

Knowing little about Flash, my main goal was to make the process of consuming .NET code as transparent as possible. As a presentation layer, Flash should have no impact on how the business layer is implemented. The biggest problem I ran into was the disparity between data types. Of particular interest was the fact that all numeric types in AMF are treated as doubles. So if you call a server side function and pass the integer value 10, it’ll get converted to 10.0. A lot of the work AMF.NET does is iron out those riples so your C# or VB.NET code can be written like you’d expect – with an int and not a double and with no extra attributes.

AMF.NET is implemented as an HttpHandler. So all you need to do is drop the DLL in your bin folder, add a line to your web.config and you’re good to go. If you want to let your server-side code get called form multiple domains, you’ll also need to dro a crossdomain.xml on the server (more info here)

It just takes a couple of lines of ActionScript to execute a remoting call:

var service:Service =
  new Service(
      ‘http://www.openmymind.net/FuelAmfGateway.aspx‘,
      null,
      ‘Company.Security, Company‘);

var pc:PendingCall = service.GetUser(1);
pc.responder =
   new RelayResponder(
         this, ‘onSuccess‘,
         ‘onFault‘);
function onSuccess(re:ResultEvent)
{   
    trace(re.result.UserName);
}

A new service is created and pointing to our HttpHandler. Notice that the service is using the standard .NET type naming to specify the type (namespace.class, assemblyName). We can then call server side method (GetUser) which accept parameters and handle the response.

So far, AMF.NET has been used on three relatively small projects here. For example, we used it in Hershey’s Take5 candy bar game (http://www.hersheys.com/take5/). As we continue to use it, we’ll build onto AMF.NET – but so far it’s proven extremely capable of doing this simple jobs – and there’s no reason to think it can handle a lot more as-is.

Both the PHP and Java projects were hugely useful in creating AMF.NET. Fiddler was also incredibly useful.

You can learn more about AMF.NET form the official homepage:
http://amfnet.openmymind.net/

This entry was posted in Grab a coffee before reading. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

13 Responses to Consuming .NET with Flash

  1. hey for those who need a nice clean sample projects, please visit my blog @ http://lakhlaniprashant.blogspot.com
    i’m going to upload a sample code there

  2. Jonathan Stevens says:

    Hey, i’m trying to use your code and i ran into a small glitch, Flash can’t find the classes you’re using in the example (Service, PendingCall). I’m sure i’m missing a small step, but if someone could point me in the right direction that would be great.

  3. karl says:

    ReTox:
    I moved openmymind.net to a different host and haven’t gotten around to moving amfnet.openmymnd.net yet. You can still download it at:
    http://sourceforge.net/projects/amfnet/

    I don’t really plan on adding any new features any time soon though.

  4. ReTox says:

    Karl, amf.net site is down. Is it relocated somewhere or completely removed?

  5. Alan Durkin says:

    Hi Karl. Does AMF.NET support ActionScript 3/AMF3? Or is it AMF0 only?
    Thanks

  6. ReTox says:

    Arghhhh, I completely forgot that – what a stupid mistake!

    I tried with typename in form of “AssemblyName.Namespace.Class”, no wander amf.net cannot find the target assembly… what an naive attempt.

    Btw, during the process of evaluating flash remoting frameworks, I installed Fluorine and it seems it is overcomplicated to use… your library is just up to task. I believe I won’t need any input custom types, basic types are enough – I think I can use xml for complex data structures

    Thanks a lot!

  7. karl says:

    ReTox:
    You can reference types in different assemblies by using the fully quantified typename – which includes the assembly name. For example, the fully quantified name for the System.String class is actually “System.String, System” …this will work as long as “System.dll” is loaded in the appDomain, which it will be if your project, which references AMF.NET also references System.dll.

    If you’re looking at inputting custom types, take a look at:
    http://amfnet.openmymind.net/howto/typed.aspx
    and
    http://amfnet.openmymind.net/howto/bitmap.aspx

    which will hopefully give you some ideas

  8. ReTox says:

    Karl,

    First, thanks for the code, it’s just what I needed.

    However, there is a problem I don’t know how to solve, regarding .net :
    class Fuel.AmfNet.Body, method Invoke() :

    Type type = Type.GetType(_type);

    the type is always null – how do you reference target assembly from which the object is instantiated (through reflection) – or how is this connection made? how does AmfNet know about external assemblies? do I need some cross-references or …?

    Thanks

  9. martin says:

    hi, need your help.

    im trying to connect to an Un-compiled .cs with this one with flash:

    import mx.remoting.NetServices;
    import mx.remoting.PendingCall;
    import mx.rpc.*;
    import mx.remoting.debug.NetDebug;

    var service:Service =
    new Service(
    ‘http://127.0.0.1/gateway.aspx’,
    null,
    ‘Company.Security.User’, ‘App_Code’);

    var pc:PendingCall = service.GetUser(1);
    pc.responder =
    new RelayResponder(
    this, “onSuccess”,
    “onFault”);

    function onSuccess(re:ResultEvent)
    {
    trace(re.result.UserName);
    }

  10. karl says:

    I haven’t looked at fluorine in a while..when I did look at it, it looked almost like a copy of Adobe’s remoting strategy. I mean, anyone who sets this up as an HttpModule vs an HttpHandler just doesn’t understand ASP.NET. If they offer better support than AMF.NET does (which is none), I’d recommend it :)

    Not too familiar with WebORB. Asked our Flash guys, seems to have a push model (that is, you can listen to it). So it seems like a mix of Red5 and Flash Remoting…not exactly apples to apples. If you need that type of push system, it’s obviously a good choice (so would Red5 I guess).

  11. Jitka says:

    Why do you think bad about Fluorine? What is your opinion on WebOrb? Thanks for your answer!

  12. karl says:

    Smith: Sorry, I haven’t played much with Flex2 / AS3. I’m hoping that it works as is. I’ll try to get one of the Flash guys @ work to build a test for me…it’s a busy month for them though..

    I’ll let you know if I find anything.

  13. smith says:

    question: I saw a jan 6th update to the amf.net site… but didnt’ see any mention of Flex2 support. Does it matter and does ActonScript 3 connect/work with your code? Sure would like to do .net serverside and Flex 2 where it fits (in pages that shouldn’t be able to be crawled/searched/indexed and that are already secured by higher level ASP.Net wrappers).

    Thanks… and and honorable effort.