Flex與.NET互操作 了解FluorineFx的環(huán)境配置(遠程對象、網(wǎng)關(guān)、通道、目的地)
在本系列文章的前面幾篇文章中所介紹的訪問Webservice的方法,也就是一種遠程對象方法,只不過他是基于WEB服務(wù)(WebServie)的遠程訪問,不是基于遠程對象(Remoting Object)的的遠程訪問。要想直接實現(xiàn)基于對象的遠程訪問是比較麻煩的,然后FluorineFx則專門為我們提供了該功能,通過 FluorineFx的核心庫來開發(fā)遠程對象(Remoting Object)服務(wù),具體是怎么實現(xiàn)的呢?
FluorineFx要求為遠程對象提供[RemotingService]標(biāo)記來提供遠程對象服務(wù),看看下面的RemotingServiceAttribute的詳細定義:
2 public sealed class RemotingServiceAttribute : Attribute
3 {
4 public RemotingServiceAttribute();
5 public RemotingServiceAttribute(string serviceName);
6 }
從上一篇文章中的示例代碼可以看出,使用.NET(c#)定義了一個Sample的遠程對象服務(wù)類,并為其指定了[RemotingService],詳細如下:
2 public class Sample
3 {
4 public Sample()
5 {
6 }
7
8 public string Echo(string text)
9 {
10 return "Gateway echo: " + text;
11 }
12 }
從上一篇文章中搭建FluorineFx與.NET的開發(fā)環(huán)境過程中就已經(jīng)出現(xiàn)過Flex客戶端調(diào)用FluorineFx的遠程對象示例,下面我們在來看看這個示例:
2 source="FlexDotNet.ServiceLibrary.Sample">
3 <mx:method name="Echo" result="onResult(event)">
4 </mx:method>
5 </mx:RemoteObject>
2 <![CDATA[
3 import mx.rpc.events.ResultEvent;
4 internal function onClick():void
5 {
6 service.Echo(txtInput.text);
7 }
8
9 internal function onResult(evt:ResultEvent):void
10 {
11 txtResult.text = evt.result.toString();
12 }
13 ]]>
14 </mx:Script>
如上可實現(xiàn)遠程對象訪問,通過Flex的非可視化組件<mx:RemoteObject>進行遠程對象連接。其中的 source屬性指定遠程對象,格式為全限定名(命名空間+類名)。destination屬性是非常重要的,這決定了Flex客戶端是否可以正確的訪問到遠對象,相關(guān)配置如下:
2 <properties>
3 <source>*</source>
4 </properties>
5 </destination>
在<mx:RemoteObject>組件內(nèi)部使用<mx:Mothod>組件配置遠程對象中的方法,詳細見本文前面部分。要真實實現(xiàn)遠程對象訪問的核心是對象的適配器和連接通道:
2 <service id="remoting-service"
3 class="flex.messaging.services.RemotingService"
4 messageTypes="flex.messaging.messages.RemotingMessage">
5 <adapters>
6 <adapter-definition id="dotnet" class="FluorineFx.Remoting.RemotingAdapter" default="true"/>
7 </adapters>
8
9 <default-channels>
10 <channel ref="my-amf"/>
11 </default-channels>
12
13 <destination id="fluorine">
14 <properties>
15 <source>*</source>
16 </properties>
17 </destination>
18 </service>
實際開發(fā)中我們可以進行自定義通信通道,默認情況下是使用FluorineFx為我們提供的默認連接通道:
2 <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
3 <endpoint uri="http://{server.name}:{server.port}/{context.root}/Gateway.aspx" class="flex.messaging.endpoints.AMFEndpoint"/>
4 <properties>
5 <!-- <legacy-collection>true</legacy-collection> -->
6 </properties>
7 </channel-definition>
8 </channels>