用WebORB实现flex + .net后台的Remoting

实现flex与后台通信最简单的方式是采用httpServic的方式,或webservice。但这两种方式都是基于文本的传输,传输效率低,
采用RemoteObject的方式,传输的内容采用AMF3格式的二进制编码,效率较高,并且能实现远程对象调用,代码的可读性,和开发效率也会有所提高。

WebORB是adobe官方推荐的实现flex与.NET后台实现 RemoteObject 的解决方案。目前WebORB完全免费

WebORB的原理介绍:
1.    在Server端,WebORB利用.NET 的HTTPHANDLE机制,HttpHandle是一种在.NET程序里显示IIS中 ISAPI功能的机制,我的理解是实际上就是一种分发机制预处理机制。类似功能的还有HttpModule,比如可以将默认需要在网站系统第一次被访问的时候就初始化以后就不需要再改变的内容利用HTTPMODULE机制重载它的OnInit方法实现。

比如使用WebORB,需要在web.config文件中增加如下配置:
<httpHandlers>
      <add verb="*" path="weborb.aspx" type="Weborb.ORBHttpHandler"/>
      <add verb="*" path="codegen.aspx" type= "Weborb.Management.CodeGen.CodegeneratorHttpHandler"/>
</httpHandlers>
这段配置表示所有.aspx的http请求在被IIS分配给aspnet_wp.exe处理后, 对于名称是weborb.aspx的请求都交由Weborb.ORBHttpHandler这个类来处理,同理所有codegen.aspx页面的请求交由Weborb.Management.CodeGen.CodegeneratorHttpHandler处理。

在Weborb.ORBHttpHandler类的内部,首先解析http请求的内容,根据flex的AMF3二进制格式解码,然后根据解码后的信息,通过.net的反射机制,将远程调用的对象转换成.NET对象,并调用client端指定的方法,然后生成对应结果集, 再编码成AMF3格式,返回给客户的

2.    在client端,flex根据编译时指定的services-config.xml配置,将RemoteObect调用时指定的destination转换成对应的url调用,在调用时生成一个对应http请求,将欲调用的类和方法按协议转换成http请求内容。

使用WEBORB的方法:
.NET版本: .NET 2.0 VS2005开发环境
flex 3.0
eclipse flex builder
1.下载WebORB,并安装
2.新建asp.net工程Flat
copy WebORB工程目录下的文件(我是安装在:C:\Inetpub\wwwroot\weborb30):
weborb.config  拷贝到根目录
diagnostics.aspx  拷贝到根目录
weborb.dll  拷贝到App_WebReferences目录
3.引用weborb.dll到flat项目
4.修改flat项目的web.config文件,增加如下配置:
<httpHandlers>
      <add verb="*" path="weborb.aspx" type="Weborb.ORBHttpHandler"/>
      <add verb="*" path="codegen.aspx" type= "Weborb.Management.CodeGen.CodegeneratorHttpHandler"/>
</httpHandlers>
5.copy WEB-INF目录下所有文件到 flat项目目录下,可以随意指定,但flex 项目中必须引用这个目录,我这里copy到: E:\wwwroot\FlexDataCenter\WEB-INF\flex
6.新建cs文件,添加如下代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// ComputeService 的摘要说明
/// </summary>

public class ComputeService
{
    
public ComputeService()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//

    }


    
public int Compute(int arg1, int arg2)
    
{
        
return arg1 + arg2;
    }

}


Compute方法实现一个计算2个参数之和的功能。

7.在eclipse中新建flex工程 HelloNet。
8.修改HelloNet项目的编译属性为:
-locale en_US -services E:\wwwroot\FlexDataCenter\WEB-INF\flex\services-config.xml

主要是需要制定-services 参数,设定services配置文件的读取路径,以便swf文件在使用RemoteObject时将对应的amf channel映射到相应的url,这个非常重要!
默认的一个channel配置如下:
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint uri="weborb.aspx" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>false</polling-enabled>
            </properties>
</channel-definition>
这个配置指定 id是my-amf的remote请求都映射到当前站点的weborb.aspx,然后交由weborb.aspx的 httphandle处理程序处理


9.修改HelloNet项目的Build Path和debug,run path : E:\wwwroot\FlexDataCenter\Flex (这是flat站点的目录)

在flex application文件中增加代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"
    xmlns
="http://www.degrafa.com/2007"
   
>
    
    
<mx:Script>
        
<![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
                                    private function getComputerInfoHandler(event : ResultEvent) : void{
                Alert.show(event.result.toLocaleString());
            }
            
            private function getFaultHandler( event : FaultEvent) : void{
                Alert.show("fault");
            }
        
]]>
    
</mx:Script>
    
        
<mx:Button label="test remote" click="compinfo.Compute(1,4);">
        
    
</mx:Button>
    
<mx:RemoteObject id="compinfo" destination="GenericDestination" 
                 source
="ComputeService"  
                 showBusyCursor
="true" >
           
<mx:method name="Compute" result="getComputerInfoHandler(event)" fault="getFaultHandler(event);"/>
</mx:RemoteObject> 

</mx:Application>

注意:RemoteObject对象 的destination表示欲调用的后台,都在remoting-config.xml配置文件中定义:
<destination id="GenericDestination">
        <properties>
            <source>*</source>
        </properties>
    </destination>
由.NET server端解析
原文地址:https://www.cnblogs.com/luluping/p/1516180.html