.NET Remoting对象Lifetime

时间:2011-05-06 07:22:02

标签: .net object remoting .net-remoting lifetime

我写了这段代码:

public class Message : MarshalByRefObject, IMessage
{
    ...

    public override object InitializeLifetimeService()
    {
        ILease leas = (ILease) base.InitializeLifetimeService();
        if (leas != null)
        {
            if(leas.CurrentState == LeaseState.Initial)
            {
                leas.InitialLeaseTime = TimeSpan.FromMilliseconds(2000);
                leas.SponsorshipTimeout = TimeSpan.Zero;
                leas.RenewOnCallTime = TimeSpan.Zero;
            }
        }
        return leas;
    }
}

重写InitializeLifetimeService是否允许在2秒后对象没有“垃圾回收”?我的意思是,如果远程访问此实例,则独立。

谢谢。

1 个答案:

答案 0 :(得分:2)

为了确保对象租约结束,您只需要将生命周期服务的轮询间隔设置为低于您设置为生命时间的2秒。

你可以在服务器对象的构造函数中执行此操作:

// just a sample value of 1 second
LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1); 

现在即使你在对象上打电话, 它仍将被收集,因为你设置了:

leas.RenewOnCallTime = TimeSpan.Zero