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)

'Declaration
<ComVisibleAttribute(True)> _
Public Interface ILease
'Usage
Dim instance As ILease

/** @attribute ComVisibleAttribute(true) */ 
public interface ILease
ComVisibleAttribute(true) 
public interface ILease
Not applicable.

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.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Permissions

Namespace RemotingSamples
   Public Class HelloService
      Inherits MarshalByRefObject

      Public Function HelloMethod(name As String) As String
         Console.WriteLine("Hello " + name)
         Return "Hello " + name
      End Function 'HelloMethod

<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.Infrastructure)> _
      Public Overrides Function InitializeLifetimeService() As Object
         Dim baseLease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)
         If baseLease.CurrentState = LeaseState.Initial Then
            ' 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)
         End If
         Return baseLease
      End Function 'InitializeLifetimeService
   End Class 'HelloService
End Namespace 'RemotingSamples

The following code is for the client assembly.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Lifetime

Namespace RemotingSamples


   Class HelloClient

      Shared Sub Main()
         ' Register the channel.
         Dim myChannel As New TcpChannel()
         ChannelServices.RegisterChannel(myChannel)
         RemotingConfiguration.RegisterActivatedClientType( _
                           GetType(HelloService), "Tcp://localhost:8085")

         Dim myTimeSpan As TimeSpan = TimeSpan.FromMinutes(10)

         ' Create a remote object.
         Dim myService As New HelloService()

         Dim myLease As ILease
         myLease = CType(RemotingServices.GetLifetimeService(myService), ILease)
         If myLease Is Nothing Then
            Console.WriteLine("Cannot lease.")
            Return
         End If

         Console.WriteLine("Initial lease time is " & myLease.InitialLeaseTime.ToString())
         Console.WriteLine("Current lease time is " & myLease.CurrentLeaseTime.ToString())
         Console.WriteLine("Renew on call time is " & myLease.RenewOnCallTime.ToString())
         Console.WriteLine("Sponsorship timeout is " & myLease.SponsorshipTimeout.ToString())
         Console.WriteLine("Current lease state is " & myLease.CurrentState.ToString())
         ' Register with a sponser.
         Dim mySponsor As 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.ToString())

         ' Renew the lease time.
         myLease.Renew(myTimeSpan)
         Console.WriteLine("Current lease time after renewal is " + _
                        myLease.CurrentLeaseTime.ToString())

         ' 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.ToString())

         ' Call the Remote method.
         Console.WriteLine("Remote method output is " + myService.HelloMethod("Microsoft"))
         myLease.Unregister(mySponsor)
      End Sub 'Main
   End Class 'HelloClient

End Namespace 'RemotingSamples

The following code is for the server assembly.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Lifetime

Namespace RemotingSamples
   Class HelloServer

      Shared Sub Main()
         Dim myChannel As New TcpChannel(8085)
         ChannelServices.RegisterChannel(myChannel)
         RemotingConfiguration.RegisterActivatedServiceType(GetType(HelloService))
         Console.WriteLine("Server started.")
         Console.WriteLine("Hit enter to terminate...")
         Console.Read()
      End Sub 'Main
   End Class 'HelloServer
End Namespace 'RemotingSamples

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: