新聞中心
今天研究了一下Castle的Remoting Facility.記錄如下:
十載的云陽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整云陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“云陽網(wǎng)站設(shè)計(jì)”,“云陽網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
微軟以前使用COM/DCOM的技術(shù)來處理分布式系統(tǒng)架構(gòu),通過Client端的Proxy代理程序來呼叫遠(yuǎn)程Server機(jī)器上的對象。.NET Framework則使用.NET Remoting或Web Services技術(shù)來實(shí)作分布式處理的工作概念;在這里針對.NET Remoting的設(shè)計(jì)架構(gòu)做一個(gè)初步的簡介和Castle整合示例。
.NET Framework提供了多種的機(jī)制來支持Remoting,如:
.利用Channel來負(fù)責(zé)信息的發(fā)送與接收。
.利用Formatter來負(fù)責(zé)在信息要通過channel發(fā)送出去之前,先將信息做適當(dāng)?shù)募用?,或于信息在通過Channel接收進(jìn)來之后,先將信息做相對的解密工作。
.利用Proxy來呼叫遠(yuǎn)程的對象執(zhí)行所要的功能呼叫。
其關(guān)系如下圖所示:
Channel 和 Formatter
在遠(yuǎn)程對象被使用之前,必須先在Server端注冊好信息發(fā)送的信道(Channel),這些Channel可通過.NET Remotin configuration file或 ChannelServices對象類別的RegisterChannel方法來注冊。
在Channel的使用上,.NET Framework支持HTTP、TCP及SMTP等通道。若使用HTTP Channel ,則使用SOAP協(xié)議來收送信息,所有的信息會被發(fā)送到SOAP Formatter中,被序列化(serialized)成XML的格式,而SOAP所需的headers也會被加入。至于使用TCP Channel者,則使用TCP協(xié)議來將信息發(fā)送到Binary Formatter中,以Binary Stream的方式來將信息發(fā)送到URI目的地。(URI : Universal Resource Identifier,類似大家所熟悉的URL)。
Activation and Proxy
Server-Side Activation
Server端在Client端要獲取Remoting對象時(shí)必需在Server端能自動啟動Remoting對象,可使用RemotingConfiguration對象類別的RegisterWellKnownServiceType方法來完成這項(xiàng)工作。
Client-Side Activation
Client端要使用遠(yuǎn)程對象之前,可使用New 或Activator 對象類別所提供的CreateInstance或GetObject方法來啟動對象并傳回Proxy,以便Client端可通過Proxy來執(zhí)行叫用遠(yuǎn)程對象的方法。
范例
以下分三個(gè)步驟來介紹
1. 建立Remoting對象
2. 在Server上初始Remoting物件
3. Client端使用Remoting對象
步驟1:建立Remoting對象
建立一個(gè)MathServer對象類別,提供Sum方法,可給予一連串的整數(shù)由Sum方法代為計(jì)算總和。程序代碼如下,并說明于后:
using System;
namespace RemoteSample.Components
{
///
/// Class1 的摘要說明。
///
public interface IRemoteMath
{
int Sum(params int[] a);
int CallCounter
{
get;
}
}
}
using System;
using RemoteSample.Components;
namespace RemoteSample.Components
{
///
/// RemoteMath 的摘要說明。
///
public class RemoteMath: MarshalByRefObject,IRemoteMath
{
private int callCounter = 0;
public RemoteMath()
{
}
#region 接口IRemoteMath的成員實(shí)現(xiàn)
///
/// 求和計(jì)算
///
///
///
public int Sum(params int[] a)
{
int sum = 0;
for (int i = 0; i <= a.Length - 1; i++)
{
sum += a[i];
}
callCounter += 1;
return sum;
}
public int CallCounter
{
get
{
return this.callCounter;
}
}
#endregion
}
}
說明:Remoting對象必須繼承自MarshalByRefObject,這樣才能通過網(wǎng)絡(luò),將對象執(zhí)行個(gè)體的參考位置傳遞給呼叫端。
步驟2:在Server上初始化Remoting對象,程序代碼如下,并說明于后:
namespace RemoteSample.Server
{
class RemoteServerMain
{
[STAThread]
internal static void Main(string[] args)
{
IWindsorContainer container = new RemotingContainer();
Console.ReadLine();
}
}
}
ServerConfig.xml文件:
remotingConfigurationFile ="../../RemotingTcpConfig.config" isServer="true" registryUri="kernel.rem" >
id="remote.math" service="RemoteSample.Components.IRemoteMath, RemoteSample.Components" type="RemoteSample.Components.RemoteMath, RemoteSample.Components" remoteserver="component" >
RemotingTcpConfig.config文件:
說明:
使用Castle 的Remoting Facillity 使用Remoting 。
1.配置指出在2133 port上要建立TCP Channel, 2133 port上要建立tcp Channel
2.
步驟3:在Client端使用Remoting對象
ClientConfig.xml
id="remote.facility" type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting" remotingConfigurationFile="../../RemotingTcpConfigClient.config" isClient="true" remoteKernelUri="tcp://localhost:2133/kernel.rem" baseUri="tcp://localhost:2133" >
id="remote.math" service="RemoteSample.Components.IRemoteMath, RemoteSample.Components" type="RemoteSample.Components.RemoteMath, RemoteSample.Components" remoteclient="component" /> RemotingTcpConfigClient.config 程序代碼如下: namespace RemoteSample.Client { /// /// RemoteClient的摘要說明。 /// public class RemoteClientMain { [STAThread] static void Main(String[] args) { IWindsorContainer container = new RemotingContainer(); IRemoteMath remoteMath = (IRemoteMath)container[typeof(IRemoteMath)] ; Console.WriteLine("Client1 TCP Call Sum method {0} Counter {1}",remoteMath.Sum(10, 20, 30),remoteMath.CallCounter); Console.WriteLine("....press a key to stop"); Console.ReadLine(); } } } 新航道雅思
分享標(biāo)題:Castle整合.NETRemoting
標(biāo)題網(wǎng)址:http://fisionsoft.com.cn/article/iiojdo.html