今天研究了一下Castle的Remoting Facility.記錄如下:
十載的云陽(yáng)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整云陽(yáng)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“云陽(yáng)網(wǎng)站設(shè)計(jì)”,“云陽(yáng)網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
微軟以前使用COM/DCOM的技術(shù)來(lái)處理分布式系統(tǒng)架構(gòu),通過(guò)Client端的Proxy代理程序來(lái)呼叫遠(yuǎn)程Server機(jī)器上的對(duì)象。.NET Framework則使用.NET Remoting或Web Services技術(shù)來(lái)實(shí)作分布式處理的工作概念;在這里針對(duì).NET Remoting的設(shè)計(jì)架構(gòu)做一個(gè)初步的簡(jiǎn)介和Castle整合示例。
.NET Framework提供了多種的機(jī)制來(lái)支持Remoting,如:
.利用Channel來(lái)負(fù)責(zé)信息的發(fā)送與接收。
.利用Formatter來(lái)負(fù)責(zé)在信息要通過(guò)channel發(fā)送出去之前,先將信息做適當(dāng)?shù)募用?,或于信息在通過(guò)Channel接收進(jìn)來(lái)之后,先將信息做相對(duì)的解密工作。
.利用Proxy來(lái)呼叫遠(yuǎn)程的對(duì)象執(zhí)行所要的功能呼叫。
其關(guān)系如下圖所示:
Channel 和 Formatter
在遠(yuǎn)程對(duì)象被使用之前,必須先在Server端注冊(cè)好信息發(fā)送的信道(Channel),這些Channel可通過(guò).NET Remotin configuration file或 ChannelServices對(duì)象類別的RegisterChannel方法來(lái)注冊(cè)。
在Channel的使用上,.NET Framework支持HTTP、TCP及SMTP等通道。若使用HTTP Channel ,則使用SOAP協(xié)議來(lái)收送信息,所有的信息會(huì)被發(fā)送到SOAP Formatter中,被序列化(serialized)成XML的格式,而SOAP所需的headers也會(huì)被加入。至于使用TCP Channel者,則使用TCP協(xié)議來(lái)將信息發(fā)送到Binary Formatter中,以Binary Stream的方式來(lái)將信息發(fā)送到URI目的地。(URI : Universal Resource Identifier,類似大家所熟悉的URL)。
Activation and Proxy
Server-Side Activation
Server端在Client端要獲取Remoting對(duì)象時(shí)必需在Server端能自動(dòng)啟動(dòng)Remoting對(duì)象,可使用RemotingConfiguration對(duì)象類別的RegisterWellKnownServiceType方法來(lái)完成這項(xiàng)工作。
Client-Side Activation
Client端要使用遠(yuǎn)程對(duì)象之前,可使用New 或Activator 對(duì)象類別所提供的CreateInstance或GetObject方法來(lái)啟動(dòng)對(duì)象并傳回Proxy,以便Client端可通過(guò)Proxy來(lái)執(zhí)行叫用遠(yuǎn)程對(duì)象的方法。
范例
以下分三個(gè)步驟來(lái)介紹
1. 建立Remoting對(duì)象
2. 在Server上初始Remoting物件
3. Client端使用Remoting對(duì)象
步驟1:建立Remoting對(duì)象
建立一個(gè)MathServer對(duì)象類別,提供Sum方法,可給予一連串的整數(shù)由Sum方法代為計(jì)算總和。程序代碼如下,并說(shuō)明于后:
using System;
namespace RemoteSample.Components
{
/// <summary>
/// Class1 的摘要說(shuō)明。
/// </summary>
public interface IRemoteMath
{
int Sum(params int[] a);
int CallCounter
{
get;
}
}
}
using System;
using RemoteSample.Components;
namespace RemoteSample.Components
{
/// <summary>
/// RemoteMath 的摘要說(shuō)明。
/// </summary>
public class RemoteMath: MarshalByRefObject,IRemoteMath
{
private int callCounter = 0;
public RemoteMath()
{
}
#region 接口IRemoteMath的成員實(shí)現(xiàn)
/// <summary>
/// 求和計(jì)算
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
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
}
}
說(shuō)明:Remoting對(duì)象必須繼承自MarshalByRefObject,這樣才能通過(guò)網(wǎng)絡(luò),將對(duì)象執(zhí)行個(gè)體的參考位置傳遞給呼叫端。
步驟2:在Server上初始化Remoting對(duì)象,程序代碼如下,并說(shuō)明于后:
namespace RemoteSample.Server
{
class RemoteServerMain
{
[STAThread]
internal static void Main(string[] args)
{
IWindsorContainer container = new RemotingContainer();
Console.ReadLine();
}
}
}
ServerConfig.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<facilities>
<facility id="remote.facility" type="Castle.Facilities.Remoting.RemotingFacility, Castle.Facilities.Remoting"
remotingConfigurationFile ="../../RemotingTcpConfig.config"
isServer="true"
registryUri="kernel.rem" >
</facility>
</facilities>
<components>
<component
id="remote.math"
service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"
type="RemoteSample.Components.RemoteMath, RemoteSample.Components"
remoteserver="component" >
</component>
</components>
</configuration>
RemotingTcpConfig.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="2133" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
說(shuō)明:
使用Castle 的Remoting Facillity 使用Remoting 。
1.配置指出在2133 port上要建立TCP Channel, 2133 port上要建立tcp Channel
2.<components>指出在Server端注冊(cè)所要使用的組件、服務(wù)的名稱及啟動(dòng)的方式。其中component表示一個(gè)執(zhí)行個(gè)體可供多個(gè)前端來(lái)呼叫,可保留其狀態(tài),另一種則為ClientActivated,一個(gè)執(zhí)行個(gè)體只能服務(wù)一個(gè)前端的呼叫,無(wú)法保留其狀態(tài)。
步驟3:在Client端使用Remoting對(duì)象
ClientConfig.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<facilities>
<facility
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" >
</facility>
</facilities>
<components>
<component
id="remote.math"
service="RemoteSample.Components.IRemoteMath, RemoteSample.Components"
type="RemoteSample.Components.RemoteMath, RemoteSample.Components"
remoteclient="component" />
</components>
</configuration>
RemotingTcpConfigClient.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="0" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
程序代碼如下:
namespace RemoteSample.Client
{
/// <summary>
/// RemoteClient的摘要說(shuō)明。
/// </summary>
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();
}
}
}
新航道雅思
網(wǎng)頁(yè)標(biāo)題:Castle整合.NETRemoting
標(biāo)題URL:http://aaarwkj.com/article10/iiojdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航、電子商務(wù)、云服務(wù)器、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)