Search
Enterprise

Programming .NET Components : Remoting - Leasing and Sponsorship (part 3) - Sponsorship Management

Published 3 min readBy Programming4us Editorial Team

6. Sponsorship Management

The sponsor in Example 2 is a completely general-purpose sponsor that simply returns the initial lease time (whatever that may be) on every renewal request. The problem with the code shown in Example 2 is that the client has to keep track of its remote objects and manually unregister each remote lease's sponsor when the application shuts down. However, you can automate this by providing a client-side sponsorship manager, in the form of the SponsorshipManager helper class:

    public class SponsorshipManager : MarshalByRefObject,
                                      ISponsor,
                                      IDisposable,
                                      IEnumerable<ILease>

    {
       public void Dispose( );
       public TimeSpan Renewal(ILease lease);
       public IEnumerator<ILease> GetEnumerator( );

       public void Register(MarshalByRefObject obj);
       public void Unregister(MarshalByRefObject obj);
       public void UnregisterAll( );     
       public void OnExit(object sender,EventArgs e);
    }

Example 3 shows the implementation of SponsorshipManager.

Example 3. The SponsorshipManager helper class


SponsorshipManager implements ISponsor by returning the initial lease time of the provided lease. In addition, it maintains in a member variable called m_LeaseList a generic linked list of all the leases it sponsors. SponsorshipManager provides the Register( ) method:

    public void Register(MarshalByRefObject obj);

Register( ) accepts a remote object, extracts the lease from it, registers SponsorshipManager as the sponsor, and adds the lease to the internal list. The Unregister( ) method of the SponsorshipManager class is defined as:

    public void Unregister(MarshalByRefObject obj);

This method removes the lease associated with the specified object from the list and unregisters SponsorshipManager as a sponsor. SponsorshipManager also provides the UnregisterAll( ) method:

    public void UnregisterAll( );

This method unregisters SponsorshipManager from all the lease objects in the list. Note that the list access is done in a thread-safe manner by locking the SponsorshipManager on every access. Thread safety is required because multiple clients on multiple threads can use the same SponsorshipManager instance to manage their remote leases. However, the clients should not bother themselves with calling Unregister( ) or UnregisterAll( )—SponsorshipManager provides an event-handling method called OnExit( ):

    public void OnExit(object sender,EventArgs e);

OnExit( ) calls UnregisterAll( ) as a response to the client application shutting down. Example 4 shows how to use SponsorshipManager by a Windows Forms client.

Example 4. Using SponsorshipManager

The client in Example 4 maintains as member variables a remote object of type MyCAO (defined in Example 10-11) and a SponsorshipManager object. The client registers the remote object with SponsorshipManager and hooks up the application OnExit event with the SponsorshipManager OnExit( ) event-handling method. SponsorshipManager does the rest—it registers itself as the sponsor, and it unregisters all the remote leases it sponsors automatically when the client shuts down. Finally, note the use of C# 2.0 iterators in implementing the IEnumerable<ILease> interface. The interface allows clients to iterate over the list of managed sponsors, if the need ever arises:

    SponsorshipManager sponsorshipManager = new SponsorshipManager( );
    //Some code to initialize sponsorshipManager, then:
    foreach(ILease lease in sponsorshipManager)
    {...}