Flex与.NET互操作(七):了解FluorineFx的环境配置(远程对象、网关、通道、目的地)

注:本文内容原本计划在上一篇《Flex与.NET互操作(六):Flex和.NET协同开发利器FluorineFx 》中写出,考虑到写在一起文章内容太长故分为两篇。

Flex中的远程对象访问,也就是服务端提供一个远程服务对象(RemotingService Object),在Flex客户端通过相应的访问技术去调用远程对象的过程。

在本系列文章的前面几篇文章中所介绍的访问Webservice的方法,也就是一种远程对象方法,只不过他是基于WEB服务(WebServie)的远程访问,不是基于远程对象(Remoting Object)的的远程访问。要想直接实现基于对象的远程访问是比较麻烦的,然后FluorineFx则专门为我们提供了该功能,通过FluorineFx的核心库来开发远程对象(Remoting Object)服务,具体是怎么实现的呢?

FluorineFx要求为远程对象提供[RemotingService]标记来提供远程对象服务,看看下面的RemotingServiceAttribute的详细定义:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 [AttributeUsage(AttributeTargets.Class,AllowMultiple = false )]
2 public sealed class RemotingServiceAttribute:Attribute
3 {
4 public RemotingServiceAttribute();
5 public RemotingServiceAttribute( string serviceName);
6 }


从上一篇文章中的示例代码可以看出,使用.NET(c#)定义了一个Sample的远程对象服务类,并为其指定了[RemotingService],详细如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 [RemotingService( " Fluorinesampleservice " )]
2 public class Sample
3 {
4 public Sample()
5 {
6 }
7
8 public string Echo( string text)
9 {
10 return " Gatewayecho: " + text;
11 }
12 }

从上一篇文章中搭建FluorineFx与.NET的开发环境过程中就已经出现过Flex客户端调用FluorineFx的远程对象示例,下面我们在来看看这个示例:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 < mx:RemoteObjectid = " service " destination = " fluorine "
2 source = " FlexDotNet.ServiceLibrary.Sample " >
3 < mx:methodname = " Echo " result = " onResult(event) " >
4 </ mx:method >
5 </ mx:RemoteObject >
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 < mx:Script >
2 <! [CDATA[
3 importmx.rpc.events.ResultEvent;
4 internal functiononClick(): void
5 {
6 service.Echo(txtInput.text);
7 }
8
9 internal functiononResult(evt:ResultEvent): void
10 {
11 txtResult.text = evt.result.toString();
12 }
13 ]] >
14 </ mx:Script >

如上可实现远程对象访问,通过Flex的非可视化组件<mx:RemoteObject>进行远程对象连接。其中的source属性指定远程对象,格式为全限定名(命名空间+类名)。destination属性是非常重要的,这决定了Flex客户端是否可以正确的访问到远对象,相关配置如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 < destinationid = " fluorine " >
2 < properties >
3 < source >*</ source >
4 </ properties >
5 </ destination >

在<mx:RemoteObject>组件内部使用<mx:Mothod>组件配置远程对象中的方法,详细见本文前面部分。要真实实现远程对象访问的核心是对象的适配器和连接通道:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 <? xmlversion = " 1.0 " encoding = " UTF-8 " ?>
2 < serviceid = " remoting-service "
3 class = " flex.messaging.services.RemotingService "
4 messageTypes = " flex.messaging.messages.RemotingMessage " >
5 < adapters >
6 < adapter - definitionid = " dotnet " class = " FluorineFx.Remoting.RemotingAdapter " default = " true " />
7 </ adapters >
8
9 < default - channels >
10 < channel ref = " my-amf " />
11 </ default - channels >
12
13 < destinationid = " fluorine " >
14 < properties >
15 < source >*</ source >
16 </ properties >
17 </ destination >
18 </ service >

实际开发中我们可以进行自定义通信通道,默认情况下是使用FluorineFx为我们提供的默认连接通道:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 < channels >
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 >

你可能感兴趣的:(.net,webservice,Flex)