Tuesday, March 3, 2009

Flash Remoting with ASP.NET

Before some time, I was involved with the project which requires a flash application to call asp.net application and gets data from there. There are obviously many ways for doing it. The way we implemented it was a bit tricky. We used Macromedia Flash Remoting MX which is a product by Adobe. Macromedia Flash Remoting MX allows a flash application to call an asp.net service (method) using remoting connection. According to the theories i've gone through on adobe, this way of calling a service of asp.net in place of using web services is more faster. The bad part of that experience was, none of the documentation samples were working directly and there are lots and lots of duplicate articles and links which discribes almost same code for sample. Finally, we manage to get through that and created a FACADE architecture at asp.net side which works like following:


  • Flash application will call gateway.aspx at the asp.net website with some parameters in the request and name of class which is remotely invoked
  • gateway.aspx will create an instance of the class described by the service parameter and passes all the request params to constructor as a collection
  • The FACADE class then reads the parameters and decides which method to call, and it calls that method from business logic layer and returns the output to flash application

This story works nicely as a theory and practical for us, and I was satisfied with the way it was working.

There is almost a year passed after that, and today morning i came across an open source project AMF.NET which is an open source version of flash remoting. I was just reading the documentation and wondering if i can implement the thing again. I was surpriced that i get through this also.

Initially, i started implementing it and i was not quite getting it, then i came across an article on http://codebetter.com/blogs/karlseguin/archive/2006/04/13/142770.aspx which has some links about the documentation. I didn't quite get some good documentation but a comment on the post also helped me.

You can download the code from here

Using Sample code:

  • Extract zip file and open the project.
  • Add a blank Default.aspx page and run it.
  • the url looks like http://localhost:3434/AMF/Default.aspx
  • Now open the facade.fla in flash 8. Also open util.as
  • In Util.as, line 12, you will require a service url as a first parameter, replace the with gateway.aspx of your application url, for example, http://localhost:3434/AMF/gateway.aspx.
  • gateway.aspx will be available in the dll in the bin directory.
  • That's it, run your AMF project and be in the debug mode, place breakpoint at getUser method of the User.cs in the app_code.
  • Run your flash application and enter username and password, and click on button. If everything goes well, you will be redirected to scene 2 and will see label over there.

After some days of writing this article, I got a comment from a user who pointed me to yet another flash remoting open source library. This one is a simple to understand. You can find the library on codeplex at http://dotnetflex.codeplex.com/ the source code is available. This library gives some better implementation of flash remoting because its much more simpler to understand than AMF. I downloaded the library from the source code section of the site and try my hand on it. I got it working in almost an hour. So hopefully developers will simply get a quick start with this. The wiki on the site provides some implementation details from asp.net side, but there is no source code sample for flash, I just tried same file as i used in the AMF.NET with some minor changes and its working nicely. Hope you all find it helpful.

You can download the file from here. You can also find a link in DOWNLOAD section of this blog also. To test the code, follow the same step described in AMF.NET section. Sample Application description:

  • Flash application displays a Login page with username and password and a login button
    clicking on login will calls a method getUser(HashTable ht) of an asp.net application from flash side
  • after getting a successfull return value, it redirects to scene2

Enhancements that can be applied at Flash Side:

  • I don't even know how to create a password textbox in flash, and i want to display a username returned from asp.net in scene2.
  • I'm working on this but if anybody can help, it would be appriciated.

Please note that I don't know flash as well as action scripts, But I'm good at learning, and i have good experience on developing asp.net applications.

Thanks and happy coding. :)

4 comments:

  1. take a look at http://dotnetflex.codeplex.com/.
    a simplier to understand and current project dealing with AMF in .Net.
    Josh

    ReplyDelete
  2. hey,
    thanks for pointing to the library, i review it and included a sample code for that library also. The sample code includes flash 8 application as well as asp.net application with code for reading params passed from flash

    ReplyDelete
  3. Wow, that's a really complex process - and, when I tried using it, I ran into quite a few problems. So, I built out a new open source project - I'm calling it Amf4Asmx. Simply build out a WebService, add the Amf4Asmx binaries, and add about half a dozen lines to your web config. Your web service's asmx file automatically acts as the flash remoting gateway!

    Good luck with all your Flash and ASP.Net projects!

    ReplyDelete
  4. Hi!!! lakhlaniprashant.blogspot.com is one of the most outstanding resourceful websites of its kind. I enjoy reading it every day. lakhlaniprashant.blogspot.com rocks!

    ReplyDelete

Please add your valuable comments about this post if it helped you. Thanks