Lifetime Leases

Objects that derive from System.MarshalByRef don’t live in memory forever. We stated earlier in this chapter that these objects have a default idle period of 5 minutes, after which time the instance will be released. A new instance will be created to service future client requests. The .NET remoting system uses a system of leases to control the lifetime of MarshalByRefObject (MBR) objects.

A lease defines the amount of time that an MBR object will be held in memory before the .NET remoting system releases the instance freeing the resources it consumes. Leases are used by the remoting server to ensure that objects not currently in use are marked for garbage collection.

When a new instance of a server-activated type is created, the server side of the remoting system will call the InitializeLifetimeService method (inherited from the MarshalByRefObject class) on the new instance. The return type of this method is the System.Runtime.Remoting.Lifetime.ILease interface, which represents an object’s lifetime lease.

The ILease instance defines the period for which the MBR object will be active. Each time a client makes a remote request to the instance, the lease will be extended. Leases can also be extended by a sponsor; a sponsor is a class that implements the System.Runtime.Remoting.Lifetime.ISponsor interface. When the lease for an MBR object has expired, the lease manager will query sponsors that have registered with the lease to see whether the lease period should be extended.

Server-activated types aren’t required to implement the ILease interface; the interface is a representation of a lease, which is maintained separately from the activated instance and managed by the server side of the remoting system.

Configuring a Lease

Table 15-3 lists the properties defined in the ILease interface.

Table 15-3. The Properties Defined in the ILease Interface

Property

Description

CurrentLeaseTime

Returns a System.Timespan representing the amount of time left before the lease expires.

CurrentState

Returns a LeaseState describing the state of the lease. (See Table 15-4.)

InitialLeaseTime

Gets or sets the initial lease period.

RenewOnCallTime

Gets or sets the period by which a client request extends the lease.

SponsorshipTimeout

Gets or sets the amount of time that the lease manager will wait for a sponsor to renew a lease.

The ILease.CurrentState property returns a LeaseState value, an enumeration whose defined states are listed in Table 15-4.

Table 15-4. The LeaseState Enumeration

Value

Description

Active

The lease is active and has not expired.

Expired

The lease has expired. Expired leases cannot be renewed.

Initial

A lease has been created but is not yet active.

None

The lease is not initialized.

Renewing

The lease has expired, and the lease manager is asking sponsors for renewal.

MBR objects can configure their own leases by overriding the InitializeLifetimeService method. The following fragment demonstrates increasing the RenewOnCallTime to 5 minutes and the InitialLeaseTime to 1 hour:

public override object InitializeLifetimeService() {
    ILease x_lease = (ILease)base.InitializeLifetimeService();

    x_lease.InitialLeaseTime = TimeSpan.FromHours(2);
    x_lease.RenewOnCallTime = TimeSpan.FromMinutes(5);

    return x_lease;
}

Classes that override the InitializeLifetimeService method must call the base class implementation to create the lease; the resulting ILease can be used to modify the properties of the lease.

For remote objects that should never expire, override the InitializeLifetimeService method to return null. Alternatively, setting the value of the Initial-LeaseTime lease property to TimeSpan.Zero has the same result.

Renewing a Lease

With the exception of ILease.CurrentLeaseTime, lease properties can be changed only when the lease is in the LeaseState.Initial mode. Once a lease is activated, property changes will be ignored. Leases can be renewed either by calling ILease.Renew directly or by the lease manager requesting a renewal from a sponsor. Leases are automatically extended by the period defined in the ILease.RenewOnCallTime period each time a client performs a remote operation on the MBR object.

Renewing a Lease Directly

Leases are obtained via the GetLifetimeService method, which is declared as a static member in the System.Runtime.Remoting.RemotingServices class or as an instance member in MBR objects. Once a lease has been obtained, the Renew method can be called to increase the lease life. The lease can be directly renewed by any class that has a reference to the MBR object, including a client. The following example shows the CountClient example class constructor updated to create a remote instance of the Server class and then renew the lease for 5 hours:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Lifetime;

class CountClient {

    CountClient() {
        // create and register the remoting channel
        HttpChannel x_channel = new HttpChannel();
        ChannelServices.RegisterChannel(x_channel);

        InitRemoteServer();

        CountServer x_server = new CountServer();

        ILease x_lease =
            (ILease)RemotingServices.GetLifetimeService(x_server);
        x_lease.Renew(TimeSpan.FromHours(5));

        DoRemoteSummation(x_server);
    }
    // other methods
}

Sponsorship Renewal

Sponsors register an interest with the lease of an MBR object through the ILease.Register method. Leases are obtained via the GetLifetimeService method declared as a static member in System.Runtime.Remoting.RemotingServices class or as an instance member in MBR objects. The following fragment demonstrates how to obtain the lease and register as a sponsor. The calling class must implement the ISponsor interface.

ILease x_lease =
    (ILease)RemotingServices.GetLifetimeService(myRemoteObject);
x_lease.Register(this);

Once registered, a sponsor waits for the lease manager to call the ISponsor.Renewal method, returning a TimeSpan indicating how much longer the lease should be extended for. Lease instances are themselves MBR objects, so clients are able to act as sponsors through the proxy mechanism. Note that if multiple sponsors are registered for an MBR instance, the lease manager will query the sponsors only until one responds with a numeric value representing a positive period of extension.

Leases can be renewed or sponsored only by using a reference to the type instance that has been published for remoting; for types that are activated by the server, the reference can be a client proxy to the remote service. If the lease for an activated type expired, sponsors aren’t automatically transferred to instances that are subsequently activated.

Setting Lease Defaults

The default values for new leases can be set using the static properties in the LifetimeServices class. Table 15-5 describes these properties.

Table 15-5. The LifetimeServices Class Properties

Property

Description

LeaseManagerPollTime

Gets or sets the period between each attempt by the lease manager to detect expired leases

LeaseTime

Gets or sets the initial lease period for new leases

RenewOnCallTime

Gets or sets the amount of time that a lease is extended each time a client makes a request to a server object

SponsorshipTimeout

Gets or sets the amount of time that the lease manager will wait for a response from a lease sponsor

Setting the defaults through the LifetimeServices class applies only to new leases; existing leases aren’t affected, and objects can specify different values in the InitializeLifetimeService method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset