Search code examples
.netobjectremoting.net-remotinglifetime

.NET Remoting object Lifetime


I wrote this code:

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;
    }
}

Does the override of InitializeLifetimeService grantee that after 2 seconds the object is no "garbage collected"? I mean, independently if this instance was remotely accessed or not.

Thank you.


Solution

  • To make sure the object lease ends you only need to set the life time service's poll interval to something lower than the 2 seconds you set as a life time.

    you can do this in the server object`s constructor:

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

    now even if you call mehods on the object , it will still be collected, because you set the:

    leas.RenewOnCallTime = TimeSpan.Zero