Renewing Leases
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.
A client can obtain the lease and extend it directly as shown in the following code example.
Dim obj As New RemoteType()
Dim lease As ILease = CType(RemotingServices.GetLifetimeService(obj), ILease)
Dim expireTime As TimeSpan = lease.Renew(TimeSpan.FromSeconds(20))
[C#]RemoteType obj = new RemoteType();
ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
TimeSpan expireTime = lease.Renew(TimeSpan.FromSeconds(20));
Sponsors
Sponsors listen for queries from a host application domain about whether the sponsor wishes to extend the lifetime 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 ask one of the clients for a lease renewal than to have all the clients pinging the remote object.
Note 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 the part of 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 could be effective for parallel problem solving, where one or more clients could give 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 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 needed to renew the lease's time, the sponsor at top of the list is asked 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, passing 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 ILease.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 ISponsor.Renewal will become the new lease time.
See Also
<lifetime> Element | Remoting Settings Schema | Remoting Example: Lifetimes | Lifetime Leases | Initializing Leases