Manage the lifetime of remote objects in depth
在《靈活管理Remote Objects生存期(lifetime)》一文中,提及了Remote Objects生存期管理的一些基本方面,已經(jīng)可以滿(mǎn)足一般基于.Net Remoting的應用。如果你覺(jué)得那些關(guān)于Remote Objects的生存期管理機制還不滿(mǎn)足需求,則可以考慮實(shí)現Client端或Server端的Sponser對象。
這里將深入討論Remote Objects生存期管理的Sponsor機制,Sponsorship is confusing, but also powful。(MSDN譯:Sponsor – 主辦方, Lease - 生存期租約,LeaseManager -租約管理器)
當Remote Object創(chuàng )建時(shí),Sponsor向Remote Object提出注冊。當Remote Object的生存期TTL要結束時(shí),LeaseManager負責與注冊的Sponsor聯(lián)系。如果Sponsor返回一個(gè)時(shí)間段TimeSpan,通知Remote Object的生存期延長(cháng)指定時(shí)間段;如果Sponsor返回TimeSpan.Zero,則Remote Object結束其生存狀態(tài)。
Sponsor對象本身是一個(gè)MarshalByRefObject對象,并且實(shí)現ISponsor接口。另外,Sponsor對象可以存放在Remote Object所在的Server端、其他的Server端、或者是Client端application,但是必須滿(mǎn)足.Net Remoting Framework可以訪(fǎng)問(wèn)到Sponsor對象。(注意:如果Sponsor存放在Client端,并且Client在firewall后面,Server端的LeaseManager就無(wú)法訪(fǎng)問(wèn)到Sponsor了。)
一個(gè)典型的Sponsor對象如下:
public class MySponsor: MarshalByRefObject, ISponsor
{
private bool NeedsRenewal()
{
// check some internal conditions
return true;
}
public TimeSpan Renewal(System.Runtime.Remoting.Lifetime.ILease lease)
{
if (NeedsRenewal())
{
return TimeSpan.FromMinutes(5);
}
else
{
return TimeSpan.Zero;
}
}
}
一般而言,對于CAO(客戶(hù)端激活對象)對象,Sponsor對象位于Client端。對于SAO(服務(wù)端激活對象)則相反。下面分別了解兩種情況的Sponsor對象:
1.Client端Sponsor
為了讓Remote Objects能夠與Client端的Sponsor打交道,需要在Client配置文件中Channel設置port屬性。如果沒(méi)有這個(gè)屬性,Channel就無(wú)法接受來(lái)自Server的回調。
<channel ref=”http” port=”0”>
Port=”0”表示允許.Net Remoting Framework選擇Client端任一空閑的port。
另外,我們知道在調用CAO遠程對象時(shí),還需要為配置文件的client項指定url屬性。
注冊Sponsor對象,避免Remote Objects過(guò)早的釋放(Client端application部分示例代碼):
string filename = "client.exe.config";
RemotingConfiguration.Configure(filename);
// 實(shí)例化CAO對象
SomeCAO cao = new SomeCAO();
// Get an object’s LifetimeService, which will be an ILease object.
ILease le = (ILease) cao.GetLifetimeService();
MySponsor sponsor = new MySponsor();
// Register the sponsor with the server object’s lease
le.Register(sponsor);
…調用Remote method或處理其他事情
// Unresister the sponsor with the server objects
le.Unregister(sponsor);
當application準備讓server釋放CAO對象實(shí)例時(shí),application通過(guò)告訴sponsor對象停止更新CAO對象實(shí)例的TTL。通常情況下,application通過(guò)調用Lease.Unregister()方法來(lái)取消sponsor對象的注冊,這樣CAO對象實(shí)例就不會(huì )與sponsor聯(lián)系了。
注意:當你決定使用client端的sponsor時(shí),需要確保server可以訪(fǎng)問(wèn)到client,這樣client就不能在firewall或proxy后面。
2.Server端Sponsor
Server端的Sponsor對象一般用來(lái)管理SAO對象的生存期,在實(shí)現方式上基本與Client端Sponsor對象一致。
但是,需要注意的是Remote sponsor對象也是MarshalByRefObject對象,因此也和Remote Objects一樣,需要管理其生存期。一般情況下,我們需要Server端sponsor對象與Client端application保持相同的生存期,當Client端application關(guān)閉時(shí),要求盡快釋放Server端sponsor對象。
Ingo Rammer《Advanced .Net Remoting》中提出了一種方案來(lái)實(shí)現Server端sponsor對象。
(1) 通過(guò)Client端后臺線(xiàn)程EnsureKeepAlive對象不斷調用sponsor.KeepAlive()方法,確保sponsor對象存活。

注意Server和Client端主要類(lèi)如上圖所示,Source Code請參考《Advance .Net Remoting》Chapter 6(Remote Object和sponsor可以為CAO或SAO對象)。
Reference:
1. Ingo Rammer, Advanced .Net Remoting.
2. Rickie, 《靈活管理Remote Objects生存期(lifetime)》
聯(lián)系客服