[Remoting] 二:遠程對象
在分布系統中,遠程對象需要跨越應用程式定義域進行傳遞,因此其表示方式會有所不同。基於效能和資料共用等原因考慮,Remoting 中遠程對象可以是 "值封送對象(MBV)" 或 "引用封送對象(MBR)"。
MBV 機制類似於 Web 無狀態請求,伺服器建立對象執行個體傳遞給通道發送到用戶端,而後伺服器端不再繼續維護其狀態和生存期。而 MBR 則在其生存期內一直存活在伺服器程式域中,用戶端只是通過代理對象來完成調用訊息傳遞,用戶端可以通過相關介面來延長遠程對象的生存期。
實現 MBV 一般通過 SerializableAttribute 特性,或者實現 ISerializable 介面。運行下面的例子,我們會發現遠程對象在用戶端程式域內,並且不是代理對象。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
namespace Learn.Library.Remoting
{
public class RemotingTest2
{
/// <summary>
/// 遠程類型
/// </summary>
[Serializable]
public class Data
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public void Where()
{
Console.WriteLine("{0} in {1}", this.GetType().Name, AppDomain.CurrentDomain.FriendlyName);
}
}
/// <summary>
/// 伺服器端代碼
/// </summary>
static void Server()
{
// 建立新的應用程式定義域,以便類比分布結構。
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
{
// 建立並註冊通道
TcpServerChannel channel = new TcpServerChannel(801);
ChannelServices.RegisterChannel(channel, false);
// 註冊遠程類型
RemotingConfiguration.ApplicationName = "test";
RemotingConfiguration.RegisterActivatedServiceType(typeof(Data));
});
}
/// <summary>
/// 用戶端代碼
/// </summary>
static void Client()
{
// 建立並註冊通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
// 註冊遠程類型
RemotingConfiguration.RegisterActivatedClientType(typeof(Data), "tcp://localhost:801/test");
// 建立遠程對象並調用其方法
Data data = Activator.CreateInstance(typeof(Data)) as Data;
data.Where();
// 判斷是否是代理
Console.WriteLine(RemotingServices.IsTransparentProxy(data));
}
static void Main()
{
Server();
Client();
}
}
}
輸出:
Data in Learn.CUI.vshost.exe
False
MBR 則要求繼承自 MarshalByRefObject 或 ContextBoundObject。繼承自 ContextBoundObject 的遠程對象,可以包含其執行內容,比如事務等等,其效能不如 MarshalByRefObject。下面的例子中,遠程對象依舊在伺服器程式域內執行,用戶端只是一個代理對象在轉寄調用。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
namespace Learn.Library.Remoting
{
public class RemotingTest2
{
/// <summary>
/// 遠程類型
/// </summary>
public class Data : MarshalByRefObject
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public void Where()
{
Console.WriteLine("{0} in {1}", this.GetType().Name, AppDomain.CurrentDomain.FriendlyName);
}
}
/// <summary>
/// 伺服器端代碼
/// </summary>
static void Server()
{
// 建立新的應用程式定義域,以便類比分布結構。
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
{
// 建立並註冊通道
TcpServerChannel channel = new TcpServerChannel(801);
ChannelServices.RegisterChannel(channel, false);
// 註冊遠程類型
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data",
WellKnownObjectMode.Singleton);
});
}
/// <summary>
/// 用戶端代碼
/// </summary>
static void Client()
{
// 建立並註冊通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
// 建立遠程對象並調用其方法
Data data = (Data)Activator.GetObject(typeof(Data), "tcp://localhost:801/data");
data.Where();
// 判斷是否是代理
Console.WriteLine(RemotingServices.IsTransparentProxy(data));
}
static void Main()
{
Server();
Client();
}
}
}
輸出:
Data in server
True
MBV 傳遞完整的副本到用戶端,而後的所有調用都是在用戶端程式域內進行,不再需要跨域的往返過程。但完整複製和序列化需要更多的資源和往返時間,因此不適合較大的對象,同時無法在多個用戶端間共用資訊。而 MBR 的所有調用都是通過代理進行的,因此往返過程比較多,但因執行過程都在伺服器端進行,其整體效能還是非常高的。在系統設計時可以針對不同的應用採取不同的選擇。