spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / csharp / webservices / chap6 / 2 To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8
[previous] [next]

Professional C# Web Services

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

ClientSponsor

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 MethodsDescription
RenewalTimeThis 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:

location of a sponsor in the client process

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 the ISponsor interface and implement this method explicitly. After writing Renew called to the console we just return the renewal time value that is defined by the RenewalTime property 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 To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: February 25, 2002
Revised: February 25, 2002


URL: http://webreference.com/programming/csharp/webservices/chap6/2/7.html