[C#]租约的Renew和“在主机应用程序域运行时始终”将对象保存在内存中
对于租约问题,有一个最简单的办法,当然示情况而定了,也许你的服务需要这种简单做法。
我是看了:
http://cnblogs.com/xenogear/archive/2004/07/19/25609.aspx
他采用的办法是“在ShowDialog之前,得到租约,然后把这个租约传到FormLogin中,在FormLogin加一个Timer,检测到快超时的时候,就Renew租约”。
我的做法:
我呢,参考的是《.NET Remoting 体系结构评估 》上讲的一段话:
public class HostRequest : MarshalByRefObject
{
///
/// 忽略默认的对象租用行为,以便“在主机应用程序域运行时始终”将对象保存在内存中.
/// 这种机制将对象锁定到内存中,防止对象被回收,但只能在主机应用程序运行
/// 期间做到这样。
///
///
public override Object InitializeLifetimeService()
{
return null;
}
/// HostRequest 连接指定的页面,并将结果返回为stream输出参数。
/// 如果我们保留状态,无论是服务器激活的 Singleton 还是客户端激活的对象,
/// 我们可能都要参与生存期管理进程:对象租用。我们已经看到很小程度的参与,
/// 使用了一种简单(且有用)的方法,就是忽略 InitializeLifetimeService 方法。
如果不忽略 InitializeLifetimeService 方法:
如果不这么做的话,服务一开始用
m_Host = (HostRequest)ApplicationHost.CreateApplicationHost(
typeof(HostRequest),
"/ActionServletRedirect",
m_strPhysicalDir);
创建出了我自己的ApplicationHost,但是隔一段时间,比如2分钟,再次调用m_Host.CreateHtmlPage时发生异常,异常描述为:“Object </af81700d_4ca5_4ea5_b715_692a78a32f6f/ShTqv7YyYlWLUSZjIFfVzPjB_5.rem> has been disconnected or does not exist at the server.”。
堆栈信息如下:
excStack "\nServer stack trace: \n at System.Runtime.Remoting.Channels.ChannelServices.CheckDisconnectedOrCreateWellKnownObject(IMessage msg)\r\n at System.Runtime.Remoting.Channels.ChannelServices.SyncDispatchMessage(IMessage msg)\n\nException rethrown at [0]: \n at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n at ActionServlet.HostRequest.CreateHtmlPage(String webPage, String query, TextWriter stream) in E:\\UM_BeijingMobile\\Coding\\PetGarden\\ActionServlet\\ActionServlet\\HostRequest.cs:line 18\r\n at ActionServlet.RedirectingCommand.Execute(SMSUpContext context) in e:\\um_beijingmobile\\coding\\petgarden\\actionservlet\\actionservlet\\redirectingcommand.cs:line 79" string