Working on project for a new client I had had the opportunity to use Flash Remoting from Flex to talk to services on a ColdFusion server without running the ColdFusion server locally on my machine. Since I am working remote these days (at home), I wanted to connect to the CFC’s on the ColdFusion server remotely from my local Flex Builder 3 working environment. I setup a simple “Hello World” example that I found over at ASFusion.
I uploaded the HelloWorldService.cfc file to the remote server in “C:\ColdFurion8\wwwroot\components\com\test\HelloWolrdService”. I then set up a RemoteObject with the endpoint property pointing to the remote ColdFusion server instead of the local host (since the example points to a local host):
<mx:remoteobject id="CFCService" endpoint="http://myremoteserver.com/flashservices/gateway/" source="components.com.test.HelloWorldService" result="getResponse(event.result)">
But I got a “Security Sandbox Error” right away from the call. This usually tells me that there needs to be a crossdomain.xml file on the root of server allowing access from my specific location or completely open using the “*” value. For more on the Flash security policy, check out plenty of info at Adobe’s site.
I uploaded the crossdomain.xml file to the root with the proper security settings (completely open in my case) and gave the server a restart just for kicks (though I don’t think its required when you change or modify the crossdomain.xml file under normal circumstances (I had some other issues unrelated to this blog post, but our admin was able to unblock those, basically the server also runs an additional web server on the same box as ColdFusion which was caused problems temporarily). So I ran the example again and received this error:
Error #1034: Type Coercion failed: cannot convert Object@359af2e1 to mx.messaging.messages.ErrorMessage
This error really stumped me because I am by no means any expert at ColdFusion and it has been years since I have had to mess around with it in any fashion. I searched and searched for this error in relation to ColdFusion and Flex. I was pulling my hair out, was it the port, was it the server address, was it the location of the CFC files, what was the issue?
I was really surprised at all the examples I found, they all were setup with both Flex and ColdFusion running on the same machine. Almost nobody was running Flex locally and accessing ColdFusion CFC’s remotely, and this is what I really needed. I eventually found a post on FlashComGuru where the author had the same exact problem as me.
It turns out that my RemoteObject had a couple of issues. First, it needed to have the destination property set, in this case ColdFusion is the default name set in services-config.xml file. Second, I had to change the endpoint property from “http://myremoteserver.com/flashservices/gateway/” to “http://myremoteserver.com/flex2gateway/”, changing the default gateway of the remote server to “flex2gateway”. So my updated RemoteObject code looks like this:
<mx:remoteobject id="CFCService" endpoint="http://myremoteserver.com/flex2gateway/" source="components.com.test.HelloWorldService" result="getResponse(event.result)">
Finally, the Hello World example worked great! However, when I tried the application I was working on to hit the application specific CFC files, I didn’t get the results I wanted. As I mentioned briefly before, the server is also running another web server on the same box as the ColdFusion server. So the web root of the web server is “inetpub/wwwroot/” and the application CFC’s were located under this web server’s directory. In order to get them to work for me, I had to move them to the ColdFusion wwwroot folder “C:\ColdFurion8\wwwroot\components\”.
I am sure there is a way to map the ColdFusion component directory to another server, but to save time and get myself back on schedule, I settled with placing my CFC file son the ColdFusion server instead, for now anyways.
I hope this post is at least a pointer to a very hard to track issue with using Flash Remoting with Flex and ColdFusion 8. The kind of lighthouse for those Flex developers who might find themselves in the same bind :).