Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

ILease Interface

Defines a lifetime lease object that is used by the remoting lifetime service.

Namespace: System.Runtime.Remoting.Lifetime
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public interface ILease
/** @attribute ComVisibleAttribute(true) */ 
public interface ILease
ComVisibleAttribute(true) 
public interface ILease

Distributed garbage collection controls when server applications can be deleted. Traditionally, distributed garbage collection uses reference counts and pinging for control. This works well when there are a few clients per object, but does not work well when there are thousands of clients per each object. The lifetime service can assume the function of a traditional distributed garbage collector, and scales well when the number of clients increases.

The lifetime service associates a lease with each remotely activated object. When the lease expires, the object is removed. A lease can specify that an object has an infinite lifetime.

Each AppDomain contains a lease manager that administers the leases in the domain. The lease manager periodically examines the leases for time expiration. If a lease has expired, it can either be canceled by removing its reference to the lease, or renewed by invoking one or more of the lease's sponsors.

A lease contains properties that determine its policies, and methods that renew the lease time. The lease exposes the ILease interface.

The following example consists of three assemblies: a client, a server, and a library shared by the client and server. Compile the library first, then the client and server. The following commands compile the Visual Basic files; to compile the C# files, use the csc command and.cs file extensions. The file names shown are only suggestions.

 vbc /t:library ILeaseShare.vb
 vbc /r:ILeaseShare.dll ILeaseServer.vb
 vbc /r:ILeaseShare.dll /r:System.Runtime.Remoting.dll ILeaseClient.vb

It is not necessary to use all Visual Basic files or all C# files; once compiled, the assemblies work together regardless of the source language. To run the example, open two command windows. Run the server first, then run the client. If you place the client and server in separate folders, you must place a copy of the shared library in each folder.

The following code is for the shared library.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Lifetime;
using System.Security.Permissions;

namespace RemotingSamples
{
   public class HelloService : MarshalByRefObject
   {
      public string HelloMethod(string name)
      {
         Console.WriteLine("Hello " + name);
         return "Hello " + name;
      }

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
      public override object InitializeLifetimeService()
      {
         ILease baseLease = (ILease)base.InitializeLifetimeService();
         if (baseLease.CurrentState == LeaseState.Initial)
         {
            // For demonstration the initial time is kept small.
            // in actual scenarios it will be large.
            baseLease.InitialLeaseTime = TimeSpan.FromSeconds(15);
            baseLease.RenewOnCallTime = TimeSpan.FromSeconds(15);
            baseLease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
         }
         return baseLease;
      }
   }
}

The following code is for the client assembly.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Lifetime;

namespace RemotingSamples
{
   class HelloClient
   {
      static void Main()
      {
         // Register the channel.
         TcpChannel myChannel = new TcpChannel ();
         ChannelServices.RegisterChannel(myChannel);
         RemotingConfiguration.RegisterActivatedClientType(
                                typeof(HelloService),"Tcp://localhost:8085");

         TimeSpan myTimeSpan = TimeSpan.FromMinutes(10);

         // Create a remote object.
         HelloService myService = new HelloService();

         ILease myLease;
         myLease = (ILease)RemotingServices.GetLifetimeService(myService);
         if (myLease == null)
         {
            Console.WriteLine("Cannot lease.");
            return;
         }

         Console.WriteLine ("Initial lease time is " + myLease.InitialLeaseTime);
         Console.WriteLine ("Current lease time is " + myLease.CurrentLeaseTime);
         Console.WriteLine ("Renew on call time is " + myLease.RenewOnCallTime);
         Console.WriteLine ("Sponsorship timeout is " + myLease.SponsorshipTimeout);
         Console.WriteLine ("Current lease state is " + myLease.CurrentState.ToString());
         // Register with a sponser.
         ClientSponsor mySponsor = new ClientSponsor();
         myLease.Register(mySponsor);
         Console.WriteLine("Wait for lease to expire (approx. 15 seconds)...");
         System.Threading.Thread.Sleep(myLease.CurrentLeaseTime);
         Console.WriteLine ("Current lease time before renewal is " + myLease.CurrentLeaseTime);

         // Renew the lease time.
         myLease.Renew(myTimeSpan);
         Console.WriteLine ("Current lease time after renewal is " + myLease.CurrentLeaseTime);

         // Call the Remote method.
         Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"));

         myLease.Unregister(mySponsor);
         GC.Collect();
         GC.WaitForPendingFinalizers();

         // Register with lease time of 15 minutes.
         myLease.Register(mySponsor,TimeSpan.FromMinutes(15));
         Console.WriteLine("Registered client with lease time of 15 minutes.");
         Console.WriteLine ("Current lease time is " + myLease.CurrentLeaseTime);

         // Call the Remote method.
         Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"));
         myLease.Unregister(mySponsor);
      }
   }
}

The following code is for the server assembly.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Lifetime;


namespace RemotingSamples
{
   class HelloServer
   {
      static void Main()
      {
         TcpChannel myChannel = new TcpChannel (8085);
         ChannelServices.RegisterChannel(myChannel);
         RemotingConfiguration.RegisterActivatedServiceType(typeof(HelloService));
         Console.WriteLine("Server started.");
         Console.WriteLine("Hit enter to terminate...");
         Console.Read();
      }
   }
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.