Accessibility

Table of Contents

Communicating between Flex and .NET

The example

The purpose of this article is to demonstrate and compare the use of three communication methods: HTTP services, web services, and Remoting. For the sake of consistency and comparison, a common example will be used throughout. While both the Flex and .NET sides will have different implementations for each method, the common goal will be to transfer an object called ClassData. ClassData contains some simple information regarding a class as well as a list of StudentData objects, representing the students in that class. In each implementation, the Flex side will pass a number representing the number of students to the server and will receive in return a ClassData object with the correct number of generated students.

The following code is the data value objects used in C#:

public class ClassData
{
     public string ClassName;
     public string TeacherName;
     public int TeacherID;
     public List<StudentData> Students;
}
public class StudentData
{
     public int StudentID;
     public string StudentName;
}

The following code is the data value objects used in ActionScript:

[RemoteClass(alias="FlexToNet.Data.ClassData")]
public class ClassData
{          
     public var ClassName:String;
     public var TeacherName:String;
     public var TeacherID:int;
     private var _students:ArrayCollection;
     public function set Students(value:ArrayCollection):void
     {
           _students = value;
     }
     public function get Students():ArrayCollection
     {
           return _students;
     }
}
[RemoteClass(alias="FlexToNet.Data.StudentData")]
public class StudentData
{
     public var StudentName:String;
     public var StudentID:int;
}

You may ignore the RemoteClass metadata tag for now, as that will be covered in the Communicating using Remoting section.

Note that for brevity we are using public fields in both C# and ActionScript. In production applications we suggest following best practices and guidelines for each environment, which includes exposing all data through properties only.