1

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.

4

1 に答える 1

2

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
于 2011-05-08T06:49:57.823 に答える