FluorineFx自带的示例都不错,就是有点不简洁,下面的代码基本上已经最简版了(环境vs2010)
1、先创建一个Web Application,然后添加FluorineFx以及FluorineFx.ServiceBrowser的引用
这二个程序集的默认位置在:
C:"Program Files (x86)"FluorineFx"Bin"net"3.5"FluorineFx.dll
C:"Program Files (x86)"FluorineFx"Bin"net"3.5"FluorineFx.ServiceBrowser.dll
2、然后添加一个DataService.cs类,写好.net的服务端代码
04 |
namespace DataTableDemo |
07 |
public class DataService |
10 |
[DataTableType( "随便填写什么" )] |
11 |
public object GetCountries( string capital) |
13 |
DataTable tbl = new DataTable(); |
14 |
tbl.Columns.Add( "ID" , typeof (System.Int32)); |
15 |
tbl.Columns.Add( "Country" , typeof (System.String)); |
16 |
tbl.Columns.Add( "Capital" , typeof (System.String)); |
18 |
tbl.Rows.Add(1, "Andorra" , "Andorra" ); |
19 |
tbl.Rows.Add(2, "United Arab" , "Abu Dhabi" ); |
20 |
tbl.Rows.Add(3, "BeiJing" , "China" ); |
22 |
if (capital.Length > 0) |
24 |
DataTable tbl2 = tbl.Clone(); |
25 |
DataRow[] drs = tbl.Select( "Capital like '%" + capital + "%'" ); |
26 |
foreach (var item in drs) |
28 |
tbl2.Rows.Add(item[0], item[1], item[2]); |
这段代码很简单,就是返回一个DataTable而已
3、添加一些配置文件
3.1、修改web.config
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
04 |
<!--添加fluorinefx配置节信息--> |
05 |
< sectionGroup name = "fluorinefx" > |
06 |
< section name = "settings" type = "FluorineFx.Configuration.XmlConfigurator, FluorineFx" requirePermission = "false" /> |
16 |
<!--添加FluorineGateway的httpMudules--> |
17 |
< add name = "FluorineGateway" type = "FluorineFx.FluorineGateway, FluorineFx" /> |
19 |
< compilation debug = "true" targetFramework = "4.0" /> |
3.2、创建WEB-INF/flex目录,并创建二个文件remoting-config.xml,services-config.xml
remoting-config.xml内容
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< service id = "remoting-service" |
03 |
class = "flex.messaging.services.RemotingService" |
04 |
messageTypes = "flex.messaging.messages.RemotingMessage" > |
06 |
<!-- DO NOT CHANGE <adapters> SECTION--> |
08 |
< adapter-definition id = "dotnet" class = "FluorineFx.Remoting.RemotingAdapter" default = "true" /> |
12 |
< channel ref = "my-amf" /> |
15 |
< destination id = "fluorine" > |
services-config.xml内容
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
04 |
< service-include file-path = "remoting-config.xml" /> |
08 |
< channel-definition id = "my-amf" class = "mx.messaging.channels.AMFChannel" > |
09 |
< endpoint uri = "http://{server.name}:{server.port}/{context.root}/Gateway.aspx" class = "flex.messaging.endpoints.AMFEndpoint" /> |
11 |
< legacy-collection >true</ legacy-collection > |
目录结构如下:
4、写Flash调用端
01 |
import org.bytearray.remoting.Service; |
02 |
import org.bytearray.remoting.PendingCall; |
03 |
import org.bytearray.remoting.events.ResultEvent; |
04 |
import org.bytearray.remoting.events.FaultEvent; |
07 |
if (root.loaderInfo.parameters.remotingGatewayPath!= null ) { |
08 |
gatewayUrl=root.loaderInfo.parameters.remotingGatewayPath+ "/Gateway.aspx" ; |
12 |
var service:Service= new Service( "DataTableDemo.DataService" ,gatewayUrl,ObjectEncoding.AMF3); |
13 |
var rpc:PendingCall=service.GetCountries( "B" ); |
14 |
rpc.addEventListener( ResultEvent.RESULT, success ); |
15 |
rpc.addEventListener( FaultEvent.FAULT, error ); |
18 |
function success( pEvt:ResultEvent ): void { |
19 |
txtResult.text= "当前网关路径:" + gatewayUrl + ""n调用成功,以下是返回的数据:"n" ; |
20 |
var len: int =pEvt.result.length; |
22 |
for (i= 0 ; i<len; i++) { |
23 |
txtResult.appendText( "ID:" +pEvt.result[i].ID+ ",Country:" +pEvt.result[i].Country+ ",Capital:" +pEvt.result[i].Capital+ ""n" ); |
28 |
function error( pEvt:FaultEvent ): void { |
29 |
txtResult.text= "当前网关路径:" + gatewayUrl + ""n调用失败,以下是详细信息:"n" ; |
30 |
txtResult.appendText( pEvt.fault.description ); |
运行截图:
示例源文件下载: http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/01.DataTable.rar