Right, I might be sold on this setup I’ve got here.
So, I’m running Flex 3 Beta 3, the new open source remoting tool from the Silent Group called FluorineFx and .NET 2.0 (under Visual Studio 2008). Now, I’d been reading through online docs, and was struggling with persistent remote objects. Basically, I had assumed that after one call to some method in a remote object, the subsequent call would be to the same object, thereby any changes made in the first call would be noticed in the second.
HOWEVER, it turns out (thanku Zoli from the Fluorine mailing list) that by default, services have a “request” scope. If you want to change this, you must modify the remoting config file in the Fluorine .NET gateway.
The code below works as intended when you change the above remote file to include “<scope>session</scope>” under the destination tag of id “fluorine”… interesting…
Note: the code below is a modification of an example by Mark Piller from the Adobe docs on Flex and .NET (pg. 6).
C# Code
City.cs
using System;
namespace FlexInterop
{
public class City
{
// The class uses public fields for brevity.
// It could work with the public properties too
public String name;
public String country;
public long population;
// Public constructor is required so instances of
// City can be created when the arrive from Flex
public City()
{
}
public City(String name, String country, long population)
{
this.name = name;
this.country = country;
this.population = population;
}
}
}
CityService.cs
using System;
using System.Collections.Generic;
using FluorineFx;
namespace FlexInterop
{
[RemotingService(“Fluorine city service.”)]
public class CityService
{
public List<City> cities;
public CityService()
{
cities = new List<City>();
cities.Add(new City(“Dallas”, “USA”, 1248816));
cities.Add(new City(“Chicago”, “USA”, 2873790));
cities.Add(new City(“Tokyo”, “Japan”, 12570000));
}
public void addCity(City newCity)
{
foreach (City city in cities)
if (city.name.Equals(newCity.name))
throw new Exception(“City with name “ + newCity.name + ” already exists”);
cities.Add(newCity);
}
public List<City> getCities()
{
return cities;
}
}
}
ActionScript 3.0 Code
City.as
package com.interop
{
[RemoteClass( alias=“FlexInterop.City”)]
public class City
{
public var name:String;
public var country:String;
public var population:Number;
}
}
Application.mxml
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application creationComplete=”init()” xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute“>
<mx:Script>
<![CDATA[
import com.interop.City;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.remoting.RemoteObject;
private var cityService:RemoteObject;
public function init():void
{
cityService = new RemoteObject( “fluorine” );
cityService.source = “FlexInterop.CityService”;
cityService.addEventListener( FaultEvent.FAULT, gotFault );
cityService.addCity.addEventListener( ResultEvent.RESULT, cityAdded );
cityService.getCities.addEventListener( ResultEvent.RESULT, gotCities );
addCity();
}
private function gotFault( evt:FaultEvent ):void
{
Alert.show( “Server reported an error – “ + evt.fault.faultString + evt.fault.faultDetail );
}
private function addCity():void
{
var city:City = new City();
city.name = “London”;
city.country = “UK”;
city.population = 10000000;
cityService.addCity( city );
}
private function cityAdded( evt:ResultEvent ):void
{
trace( “city added, refreshing cities list” );
getCities();
}
private function getCities():void
{
trace( “getting all cities” );
cityService.getCities();
}
private function gotCities( evt:ResultEvent ):void
{
trace( “received all cities” );
// display in a data grid
cities.dataProvider = evt.result;
}
]]>
</mx:Script>
<mx:DataGrid id=”cities” />
</mx:Application>
Recent Comments