Initializing Leases

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the  Windows Communication Foundation (WCF).

When a MarshalByRefObject derived class is instantiated, the .NET Remoting framework calls the object's InitializeLifetimeService method. This method returns a lease object that contains the default lifetime values for the remote object. You can modify these default values by specifying alternative values in the server application configuration file (using the <lifetime> Element) or by overriding InitializeLifetimeService.

If you specify alternative values in a configuration file, be aware that this changes the default lifetime values for all objects the server application hosts. Overriding InitializeLifetimeService allows you to change the default lifetime values for a specific class.

Overriding InitializeLifetimeService

When overriding InitializeLifetimeService you must check the value of the CurrentState. You can only change the lease values when CurrentState equals Initial.

The only call that affects the lifetime service is the call to InitializeLifetimeService from the .NET remoting infrastructure, which activates the lease. Other code can call InitializeLifetimeService and create a lease, but that lease stays in its initial state until it is returned to the .NET remoting infrastructure.

If InitializeLifetimeService returns null, the object's lifetime is infinite and is not garbage collected until the hosting application domain is unloaded.

The implementation of InitializeLifetimeService normally calls the corresponding method of the base class to retrieve the existing lease for the remote object. If the object has never been marshaled, the lease returned is in its initial state and the lease properties can be set. Once the object has been marshaled, the lease goes from the initial to the active state and any attempt to initialize the lease properties is ignored and an exception is thrown. InitializeLifetimeService is called when the remote object is activated. A list of sponsors for the lease can be supplied with the activation call and additional sponsors can be added at any time while the lease is active.

For information about how to override the InitializeLifetimeService method, see How to: Override the InitializeLifetimeService Interface.

See Also


How to: Override the InitializeLifetimeService Interface


Renewing Leases
Remoting Example: Lifetimes
Lifetime Leases