Partilhar via


Renewing 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).

Once a lease has been created, the only property on the lease that can be altered is ILease.CurrentLeaseTime. There are two ways to renew a lease: a client can call ILease.Renew directly, or a sponsor can be contacted and asked to renew the lease.

Sponsors

Sponsors listen for requests from a host application domain to extend the lease of a particular object. Sponsors implement ISponsor and are registered with the lease manager by obtaining a reference to the lease and then calling ILease.Register. In general, when there are many clients per remote object, it is more efficient to have the remote object send a request to one of the clients for a lease renewal rather than have all the clients sending requests to the remote object.

NoteNote

Starting with version 1.1 of the .NET Framework, registering a sponsor to participate in a server object's lifetime requires the Full automatic deserialization setting on both the server application domain and the sponsor. For details, see Automatic Deserialization in .NET Remoting.

Using sponsors also provides a dynamic renewal policy among a large number of clients. This can be effective for parallel problem solving, where one or more clients gives multiple remote objects a problem to solve. When a remote object returns a solution, the sponsor is notified and allows all other remote object leases to expire.

This approach is also useful for migrating remote objects to a different machine because the remote object contacts the client from its current location rather then the client having to find it.

It is also important to note that the sponsor might be difficult to reach if it is located across a large network or even on the Internet with many firewalls. You can counter this by having backup sponsors, or placing the sponsors close to the host application domain where they can be reached with some degree of confidence.

The host application domain's lease manager maintains a list of the sponsors. When a sponsor is required to renew the lease's time, the sponsor at top of the list is requested to renew the time. If the sponsor does not respond in the ILease.SponsorshipTimeout time span, it is removed from the list and the next sponsor on the list is called.

Using a Sponsor to Renew a Lease

Sponsors participate in lifetime leases by obtaining the lease for a particular object reference, registering themselves as a sponsor, and waiting for the remote lease manager to call ISponsor.Renewal. An object lease is obtained by calling RemotingServices.GetLifetimeService, which takes the object for which the lease is required as a parameter. This call is a static method of the RemotingServices class. If the object is local to the application domain, the parameter to this call is a local reference to the object and the lease returned is a local reference to the lease. If the object is remote, the proxy is passed as a parameter. Note that the lease itself is a marshal-by-reference (MBR) object, so when you obtain a lease for a remote object, you get a proxy to the lease. Therefore, when you make calls to the methods on the lease you are making remote calls to the server process.

You then register the sponsor with the remote lease manager by calling Register and passing the sponsor and an optional TimeSpan object, if the object has just been created.

When the lease for this object expires, the lease manager might call back to your remote sponsor. Your sponsor's return value for its implementation of Renewal becomes the new lease time.

See Also

Tasks

How to: Renew a Lease

Reference

<lifetime> Element
Remoting Settings Schema

Concepts

Remoting Example: Lifetimes
Lifetime Leases
Initializing Leases