| home / programming / csharp / webservices / chap6 / 2 | [previous] [next] |
|
The third option to change the values for the lifetime services is by implementing a sponsor. A sponsor for an Indy cart team gives money to the team, so that the team can do its job. This is similar to remoting: a sponsor extends the lifetime of an object so that the object can do its job. Without a sponsor, the client would have to make method calls on the remote object to keep it alive.
The .NET Remoting runtime uses the ISponsor interface to extend the lifetime of remote objects
using sponsoring. ISponsor defines the method Renewal(), which is called by the
.NET remoting infrastructure to extend the lease time of the current object. This method has the following
signature:
TimeSpan Renewal(ILease lease);
With the lease argument we can read the current configuration and the actual status of the lease time; with the return value we have to define the additional lease time for the object.
The class ClientSponsor provides a default implementation for a sponsor.
ClientSponsor implements the interface ISponsor. ClientSponsor
also derives from MarshalByRefObject, so it can be called across the network:
| ClientSponsor Properties and Methods | Description |
RenewalTime | This property defines the time that is used to extend the leasing time of the remote object. |
Register() | With this method we can register a specified remote object with the sponsor, so that the sponsor answers requests for this object. The challenge with this method is that it must be called in the process of the remote object, because it adds the managed object to the sponsor table of this process. |
Unregister() | Sponsorship is canceled. Unregister() removes the remote object from the sponsor table. |
Renewal() | Renewal() is the method that is called by the runtime to extend the lifetime. |
Close() | Close() clears the list of objects managed with this sponsor. |
ClientSponsor Example
A useful place to put the sponsor is in the client process as shown in the next diagram. This has the advantage that the sponsor will not extend the lifetime of the remote object when the client is not reachable:

The client must create an instance of the ClientSponsor class, and call the Register()
method where the remote object must be registered. As a result of this registration, the remoting runtime
calls the Renewal() method that is defined with the ISponsor interface to renew
the lifetime.
To demonstrate the calls to the sponsor in action I have created a MySponsor class that
derives from ClientSponsor in the client application. The only thing that this class should
do differently from the base class is answering Renewal() requests and writing
Renew called to the console.
Renewal()is not declared virtual in the base class so we have to add an implements declaration of theISponsorinterface and implement this method explicitly. After writingRenew calledto the console we just return the renewal time value that is defined by theRenewalTimeproperty of the client sponsor.
// SimpleClient.cs
// ...
public class MySponsor: ClientSponsor, ISponsor
{
TimeSpan ISponsor.Renewal(ILease lease)
{
Console.WriteLine("Renewal called");
return this.RenewalTime;
}
}
The following code example shows the Main() method of the client that creates a sponsor
and sets the RenewalTime of the sponsor:
// SimpleClient.cs
// ...
static void Main(string[] args)
{
RemotingConfiguration.Configure("SimpleClient.exe.config");
MyRemoteObject obj = new MyRemoteObject(333);
MySponsor sponsor = new MySponsor();
sponsor.RenewalTime = TimeSpan.FromMinutes(2);
sponsor.Register(obj);
The sponsor is a remote object created in the client process. Here, the server acts as a client when doing the renewal, and the client acts as server when the sponsor is called. To support this, the channel for the client must support a client and a server channel, and the same is also true for the channel running on the server.
| home / programming / csharp / webservices / chap6 / 2 | [previous] [next] |
Created: February 25, 2002
Revised: February 25, 2002
URL: http://webreference.com/programming/csharp/webservices/chap6/2/7.html