
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
很长一段时间里,URWPGSim2D的调试中总是碰到一个远程对象相关错误。加载策略运行时,如果有提示对话框弹出没有注意到过了一段时间,或下了断点在某处,停下来思考了一段时间,然后继续运行时会弹出类似如下错误提示:
System.Runtime.Remoting.RemotingException: Object '/76e7cd41_2cd2_4e89_9c03_fae752ec4d59
/zb_uualy_cm6kwizjlentfdl_3.rem' has been disconnected or does not exist at the server.
其中单引号内的内容每次都不相同。
为实现策略dll的独立加载,使用了跨应用程序域(AppDomain)技术,这个问题必和AppDomain有关,而且能猜测,应该是和对象的生存期有关。经过一番搜寻,找到这篇文献:
由上述文献得知,错误是MarshalByRefObject派生类对象的生存期(这里应该是指不活动就销毁的时间间隔)默认为5分钟所致。URWPGSim2D的客户端和服务端加载独立的策略dll文件,用到了两个继承于MarshalByRefObject的类。一个是用于加载策略的工厂类StrategyInterfaceFactory,一个是策略类Strategy。在这两个类中重载(override)InitializeLifetimeService方法,让其返回null而非一个ILease实现,即可让这两个类的对象生存期无限。
/// <summary>
/// override the InitializeLifetimeService to return null instead of a valid ILease implementation
/// to ensure this type of remote object never dies
/// </summary>
/// <returns>null</returns>
public override object InitializeLifetimeService()
{
//return base.InitializeLifetimeService();
return null; // makes the object live indefinitely
}
/// override the InitializeLifetimeService to return null instead of a valid ILease implementation
/// to ensure this type of remote object never dies
/// </summary>
/// <returns>null</returns>
public override object InitializeLifetimeService()
{
//return base.InitializeLifetimeService();
return null; // makes the object live indefinitely
}