利用fluorineFx将DataTable从.Net传递到Flash

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的服务端代码

using System.Data;

using FluorineFx;



namespace DataTableDemo

{

    [RemotingService]

    public class DataService

    {



        [DataTableType("随便填写什么")]

        public object GetCountries(string capital)

        {

            DataTable tbl = new DataTable();

            tbl.Columns.Add("ID", typeof(System.Int32));

            tbl.Columns.Add("Country", typeof(System.String));

            tbl.Columns.Add("Capital", typeof(System.String));



            tbl.Rows.Add(1, "Andorra", "Andorra");

            tbl.Rows.Add(2, "United Arab", "Abu Dhabi");

            tbl.Rows.Add(3, "BeiJing", "China");



            if (capital.Length > 0)

            {

                DataTable tbl2 = tbl.Clone();                

                DataRow[] drs =  tbl.Select("Capital like '%" + capital + "%'");

                foreach (var item in drs)

                {

                    tbl2.Rows.Add(item[0], item[1], item[2]);

                }

                return tbl2;

            }



            return tbl;



        }

    }

}

这段代码很简单,就是返回一个DataTable而已

3、添加一些配置文件

  3.1、修改web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

	<configSections>

    <!--添加fluorinefx配置节信息-->

    <sectionGroup name="fluorinefx">

			<section name="settings" type="FluorineFx.Configuration.XmlConfigurator, FluorineFx" requirePermission="false"/>

		</sectionGroup>

	</configSections>

	<fluorinefx>

		<settings>

		</settings>

	</fluorinefx>

  

	<system.web>

		<httpModules>

      <!--添加FluorineGateway的httpMudules-->

      <add name="FluorineGateway" type="FluorineFx.FluorineGateway, FluorineFx"/>

		</httpModules>

		<compilation debug="true" targetFramework="4.0" />

	</system.web>

</configuration>



  3.2、创建WEB-INF/flex目录,并创建二个文件remoting-config.xml,services-config.xml

remoting-config.xml内容

<?xml version="1.0" encoding="UTF-8"?>

<service id="remoting-service"

    class="flex.messaging.services.RemotingService"

    messageTypes="flex.messaging.messages.RemotingMessage">



	<!-- DO NOT CHANGE <adapters> SECTION-->

    <adapters>

        <adapter-definition id="dotnet" class="FluorineFx.Remoting.RemotingAdapter" default="true"/>

    </adapters>



    <default-channels>

        <channel ref="my-amf"/>

    </default-channels>



  <destination id="fluorine">

    <properties>

      <source>*</source>

    </properties>

  </destination>



</service>



services-config.xml内容

<?xml version="1.0" encoding="utf-8" ?> 

<services-config>

    <services>

  		<service-include file-path="remoting-config.xml" /> 

    </services>

    

    <channels>

        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">

            <endpoint uri="http://{server.name}:{server.port}/{context.root}/Gateway.aspx" class="flex.messaging.endpoints.AMFEndpoint"/>

            <properties>

				      <legacy-collection>true</legacy-collection>

            </properties>

        </channel-definition>

    </channels>

</services-config>

目录结构如下:

利用fluorineFx将DataTable从.Net传递到Flash

4、写Flash调用端

import org.bytearray.remoting.Service;

import org.bytearray.remoting.PendingCall;

import org.bytearray.remoting.events.ResultEvent;

import org.bytearray.remoting.events.FaultEvent;



var gatewayUrl:String="http://localhost:7796/Gateway.aspx";

if (root.loaderInfo.parameters.remotingGatewayPath!=null) {

	gatewayUrl=root.loaderInfo.parameters.remotingGatewayPath+"/Gateway.aspx";

}





var service:Service=new Service("DataTableDemo.DataService",gatewayUrl,ObjectEncoding.AMF3);

var rpc:PendingCall=service.GetCountries("B");

rpc.addEventListener( ResultEvent.RESULT, success );

rpc.addEventListener( FaultEvent.FAULT, error );





function success( pEvt:ResultEvent ):void {

	txtResult.text="当前网关路径:" + gatewayUrl + "\n调用成功,以下是返回的数据:\n";

	var len:int=pEvt.result.length;

	var i:int=0;

	for (i=0; i<len; i++) {

		txtResult.appendText("ID:"+pEvt.result[i].ID+",Country:"+pEvt.result[i].Country+",Capital:"+pEvt.result[i].Capital+"\n");

	}

	

}



function error( pEvt:FaultEvent ):void {

	txtResult.text="当前网关路径:" + gatewayUrl + "\n调用失败,以下是详细信息:\n";

	txtResult.appendText( pEvt.fault.description );

}

运行截图:

利用fluorineFx将DataTable从.Net传递到Flash

示例源文件下载: http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/01.DataTable.rar

你可能感兴趣的:(Datatable)