2010年6月4日星期五

透過 WebORB 使用 Flex Remoting 與 .NET 溝通

今天研究了一下透過 WebORB 讓 Flex 與 .NET 溝通,並找出最基本需要佈署的 WebORB 檔案有哪些,以下是簡單的步驟記錄:

1.
下載 WebORB,安裝在 IIS 中,重點在於接下來要從安裝的目錄中,複製哪些檔案到自己的網站專案中。

2.
複製以下資料夾與檔案
/bin/*.dll
/console/*.*
/WEB-INF/flex/*.xml
/weborbassets/*.*

diagnostics.aspx
weborb.config
weborbconsole.html

有些檔案是為了 console 管理介面用的

3.
複製 web.config 的部分內容

<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="weborb.aspx" type="Weborb.ORBHttpHandler"/>
<add verb="*" path="codegen.aspx" type="Weborb.Management.CodeGen.CodegeneratorHttpHandler"/>
</httpHandlers>
</system.web>
</configuration>


4.
瀏覽 http://localhost:3000/weborb.aspx 應會自動導到 http://localhost:3000/weborbconsole.html,在此管理介面可佈署 *.dll,也可將想佈署的 *.dll 自己放到網站 bin 資料夾即可。

這裡的 port 是依照自己使用 VS.Net Express 設定的 Developer WebServer 決定,若使用 IIS 亦可使用 80 或其他 port。

5.
網站專案中,任意開發一個 class,譬如:

MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemoteClassLib
{
public class MyClass
{
public String greeting(String who){
return "hello, " + who;
}
}
}


編譯後,應會在 bin 中,出現 RemoteClassLib.dll,若在 WebORB 管理介面中,也會看到這個套件已經被佈署,並可測試 MyClass 的 greeting()。

6.
開發 Flex 的部分之前,先準備幾個 xml 檔,描述 Flex Remoting 的位置,晚點要給 Flex 編譯時包進去

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://localhost:3000/weborb.aspx" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
</channels>        
</services-config>


remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="Weborb.V3Types.Core.RemotingHandler"
messageTypes="flex.messaging.messages.RemotingMessage">

<adapters />

<default-channels>
<channel ref="my-amf"/>
</default-channels>


<destination id="GenericDestination">
<properties>
<source>*</source>
</properties>
</destination>   
</service>


7.
開發 Flex 程式,看你喜歡用 mx.rpc.remoting.mxml.RemoteObject 或 mx.rpc.remoting.RemoteObject 來連接都可以:

WebORBSimpleTestClient.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import RemoteClassLib.MyClass;

import mx.controls.Alert;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

private function greetingHandler(event:ResultEvent):void
{

var returnValue:String = event.result as String;

Alert.show(returnValue);
}

private function onFault (event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</mx:Script>
<mx:creationComplete>
<![CDATA[
var remoteObject:RemoteObject  = new RemoteObject("GenericDestination");
remoteObject.source = "RemoteClassLib.MyClass";

remoteObject.greeting.addEventListener("result",greetingHandler);

remoteObject.addEventListener("fault", onFault);

var asyncToken:AsyncToken = remoteObject.greeting("Ben Chang");
]]>
</mx:creationComplete>
</mx:Application>


8.
測試,運氣好的話,就可以看到 Alert 出 "Hello, Ben Chang" 的畫面。

0 意見: