Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

ColdFusion and Silverlight Playing Together Via AMF

December 16, 2008 · 4 Comments

Click Here To View The Sample

I've spent a lot of time over the past couple of months digging deeply into Silverlight (more about why in a future post). One of thing things that has constantly frustrated me with Silverlight is the fact that you're pretty much limited to some sort of text based transfer protocol, be it XML, JSON, SOAP, whatever. I guess Adobe has spoiled me with using AMF for everything. Well, after some digging, I came across FluorineFx, an open source library that allows for remoting within the .NET framework.

Getting it setup is pretty simple. You just run the installer, then add a reference to the FluroineFx.dll in your project. Once that was all set, it took me a bit of digging to get the right setup for remoting. I had my destination, my cfc path, my method name, all the stuff needed to get this working right. So, I dove into the samples, and applied them to my ColdFusion remoting setup. The results: an AMF call to ColdFusion that returned data back properly. Let's take a look at the code:

Page.xaml:
<UserControl x:Class="SilverlightColdFusion.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="600" Height="300">

    <Grid x:Name="LayoutRoot" Background="AliceBlue">
    <StackPanel Orientation="Vertical">
    <Button x:Name="factButton" Content="Click To Load Fact" Height="25" Width="75" Click="loadBtn_Click"/>

    <StackPanel HorizontalAlignment="Left" Orientation="Vertical" Margin="15">
    <TextBlock x:Name="tLabel" Text="Your Fact: "/>
    <TextBlock x:Name="tFact" />
    </StackPanel>
    </StackPanel>
    </Grid>
   </UserControl>

This is a pretty straightforward, no nonsense XAML file. We've basically got a button that will fire our call to ColdFusion and a TextBlock that will display the result of the call.

Page.xaml.cs
using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Net;
   using System.Windows;
   using System.Windows.Controls;
   using System.Windows.Documents;
   using System.Windows.Input;
   using System.Windows.Media;
   using System.Windows.Media.Animation;
   using System.Windows.Shapes;

   using FluorineFx;
   using FluorineFx.Net;
   using FluorineFx.Messaging.Api;
   using FluorineFx.Messaging.Api.Service;
   using FluorineFx.AMF3;

   namespace SilverlightColdFusion
   {
    public partial class Page : UserControl
    {
    NetConnection _netConnection;

    public Page()
    {
    InitializeComponent();

    //Create NetConnection Client    _netConnection = new NetConnection();
    _netConnection.ObjectEncoding = ObjectEncoding.AMF3;
    _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus);
    //Define the AMF Gateway URL    _netConnection.Connect("http://www.infoaccelerator.net:80/flex2gateway");

    }

    void _netConnection_NetStatus(object sender, NetStatusEventArgs e)
    {
    string level = e.Info["level"] as string;

    }
   
         //The button click handler from the XAML button     private void loadBtn_Click(object sender, RoutedEventArgs e)
    {
    //Define the channel, the destination, the cfc path, the method, the result handler, then an array of the args (if applicable)    _netConnection.Call("my-cfamf","ColdFusion","chuckFacts.FactService","getCurrentFact",new GetFactsHandler(this));
    }

    //Inner class as our result handler, see the above line    public class GetFactsHandler : IPendingServiceCallback
    {
    Page _page;

    public GetFactsHandler(Page page) {
    _page = page;
    }

    //The function handle the result    public void ResultReceived(IPendingServiceCall call) {
    string fact = call.Result as string;
    _page.Bind(fact);
    }
    }

    public void Bind(string fact) {
    //We use Dispatcher to write back to the parent app, otherwise we'd throw a thread exception.    Dispatcher.BeginInvoke(delegate()
    {
    tFact.Text = fact;
    });

    }
    }
   }

Open up your favorite AMF debugging proxy, be it Charles or Service Capture and take a look at the traffic passing back and forth between the sample application below and the server. You'll see perfectly formatted AMF going back and forth between Silverlight and ColdFusion. My next post will be a follow-up on how to send typed objects across the wire to Silverlight.

Click Here To View The Sample

Tags: ColdFusion · Flex · Silverlight · Universal Mind

4 responses so far ↓

  • 1 Andrew Powell // Dec 16, 2008 at 11:30 AM

    Someone asked me why FluorineFx over WebORB. Well, it's quite simple really, FluorineFx's docs were better.
  • 2 Mark Piller // Dec 16, 2008 at 11:33 AM

    For those who's challenged in finding info for WebORB, here's a blog post describing exactly the same functionality using WebORB:<br />http://www.themidnightcoders.com/blog/2008/12/inter-process-communication-using-amf.html
  • 3 Jane Carter // Feb 13, 2009 at 4:35 PM

    Overall, do you have any sense of whether Silverlight or Flex is easier to work with on the client-side, assuming that we have CF on the server? Being able to write the client-side code without having to learn ActionScript would be a huge plus.
  • 4 Andrew Powell // Feb 14, 2009 at 11:21 AM

    @Jane If you're already a .NET shop, then Silverlight makes more sense, otherwise Flex is the best answer. Assuming you have no talent that knows either one, I'd say Flex is the way to go because there is much more community support for it.

Leave a Comment