MarshalByRefObject provides some methods to work with lifetime.
public MarshalByObject { public Object GetLifetimeService(); public virtual Object InitializeLifetimeService(); }
This method is used return a lifetime service object that is used to control the lifetime policy to the object. For the default Lifetime service this will be an object of type ILease.
Return Object to control lifetime Service.
This method is used return lifetime service object. This method can be overridden to return a LifetimeService object with properties unique to this object. For the default Lifetime service this will be an object of type ILease.
Return: Object to control lifetime Serivice
The ILease interface contains the publicly callable methods in the Lease class.
public interface ILease { void Register(ISponsor obj, TimeSpan renewalTime); void Register(ISponsor obj); void UnRegister(ISponsor obj); TimeSpan Renew(TimeSpan renewalTime); TimeSpan RenewOnCallTime {get; set;} TimeSpan SponsorshipTimeout {get; set;} TimeSpan InitialLeaseTime {get; set;} TimeSpan CurrentLeaseTime {get;} LeaseState CurrentState {get;} }
Register a sponsor for the lease with a renewal time.
Register a sponsor for the lease with no renewal time.
Unregister a sponsor for the leae.
Renew the lease for the specified time.
The renew on call time for the lease.
The sponsorship timeout for the lease.
The initial lease time.
The current lease time.
The current lease state.
Each sponsor supports the ISponsor interface. It is used by the LDGC to call back to the Sponsor.
interface ISponsor { TimeSpan Renewal(); }
The LDGC asks a sponsoring client to renew the lease for the specified service. The sponsor returns the additional lease time.
Lease States.
public enum LeaseState { Null = 0, Initial = 1, Active = 2, Renewing = 3, Expired = 4, }
The lease is not initialized.
Lease has been created, but not yet active.
The lease is active and has not expired.
Lease has expired and is looking for sponsorship.
Lease has expired and cannot be renewed.
Used to set and get lifetime settings. Time set for an AppDomain. They must be set before the first lease is created (the first object is marshaled which creates a lease).
public class LifetimeServices { public static TimeSpan LeaseTime {get;set} public static TimeSpan RenewOnCallTime {get;set} public static TimeSpan SponsorshipTimeout {get;set} public static TimeSpan LeaseManagerPollTime {get;set} }
Initial Lease Time span for appdomain. Default 5 minutes.
Initial renew on call time span for appdomain. Default 2 minutes.
Initial sponsorshiptimeout for appdomain. Default 2 minutes.
Initial sponsorshiptimeout for appdomain. Default 10 seconds.