RemotingServices.GetLifetimeService Method

Returns a lifetime service object that controls the lifetime policy of the specified object.

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

'Declaration
Public Shared Function GetLifetimeService ( _
	obj As MarshalByRefObject _
) As Object

Parameters

obj
Type: System.MarshalByRefObject

The object to obtain lifetime service for.

Return Value

Type: System.Object
The object that controls the lifetime of obj.

ExceptionCondition
SecurityException

The immediate caller does not have infrastructure permission.

For the default lifetime service the returned object will be an object of type ILease. If the obj parameter is Nothing, the method returns Nothing.

The following code example demonstrates how to use the GetLifetimeService method to get a lifetime lease for the specified object.

Imports System
Imports System.Collections
Imports System.Net.Sockets
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Permissions
Imports TimerService



Public Class TimerClient
    Inherits MarshalByRefObject
    Implements ISponsor

    Public Shared Sub Main() 
        Dim myClient As New TimerClient()

    End Sub 'Main

    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)> _
    Public Sub New() 
        ' Registers the HTTP Channel so that this client can receive 
        ' events from the remote service. 
        Dim serverProv As New BinaryServerFormatterSinkProvider()
        serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
        Dim clientProv As New BinaryClientFormatterSinkProvider()

        Dim props As New Hashtable()
        props("port") = 0
        Dim channel As New HttpChannel(props, clientProv, serverProv)
        ChannelServices.RegisterChannel(channel)

        Dim remoteType As New WellKnownClientTypeEntry(GetType(TimerService), "http://localhost:9000/MyService/TimerService.soap")
        RemotingConfiguration.RegisterWellKnownClientType(remoteType)

        Dim groupTimer As New TimerService()
        groupTimer.MinutesToTime = 4.0

        ' Registers this client as a lease sponsor so that it can 
        ' prevent the expiration of the TimerService. 
        Dim leaseObject As ILease = CType(RemotingServices.GetLifetimeService(groupTimer), ILease)
        leaseObject.Register(Me)

        ' Subscribes to the event so that the client can receive notifications from the server. 
        AddHandler groupTimer.TimerExpired, AddressOf OnTimerExpired
        Console.WriteLine("Connected to TimerExpired event")

        groupTimer.Start()
        Console.WriteLine("Timer started for {0} minutes.", groupTimer.MinutesToTime)
        Console.WriteLine("Press enter to end the client process.")
        Console.ReadLine()

    End Sub 'New 


    Private Sub OnTimerExpired(ByVal [source] As Object, ByVal e As TimerServiceEventArgs) 
        Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message)

    End Sub 'OnTimerExpired

    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)> _
    Public Function Renewal(ByVal lease As ILease) As TimeSpan Implements ISponsor.Renewal
        Console.WriteLine("TimerClient: Renewal called.")
        Return TimeSpan.FromMinutes(0.5)

    End Function 'Renewal
End Class 'TimerClient

To compile and run this example, you will need to compile and run a server, timerserver.exe, and compile a shared library, timerservice.dll.

The source for timerserver.exe follows:

Imports System
Imports System.Collections
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Lifetime
Imports System.Timers



Public Class TimerServer

    Public Shared Sub Main() 

        Dim serverProv As New BinaryServerFormatterSinkProvider()
        serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
        Dim clientProv As New BinaryClientFormatterSinkProvider()

        Dim props As New Hashtable()
        props("port") = 9000
        Dim channel As New HttpChannel(props, clientProv, serverProv)
        ChannelServices.RegisterChannel(channel)
        RemotingConfiguration.RegisterWellKnownServiceType(GetType(TimerService), "MyService/TimerService.soap", WellKnownObjectMode.Singleton)

        Console.WriteLine("Press enter to end the server process.")
        Console.ReadLine()

    End Sub 'Main
End Class 'TimerServer

The source for timerservice.dll follows:

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Permissions
Imports System.Timers


' Define the event arguments
<Serializable()>  _
Public Class TimerServiceEventArgs
    Inherits EventArgs
    Private m_Message As String 

    Public Sub New(ByVal message As String) 
        m_Message = message

    End Sub 'New 


    Public ReadOnly Property Message() As String  
        Get 
            Return m_Message
        End Get 
    End Property 
End Class 'TimerServiceEventArgs


' Define the delegate for the event 
Public Delegate Sub TimerExpiredEventHandler(ByVal sender As Object, ByVal e As TimerServiceEventArgs) 

' Define the remote service class 

Public Class TimerService
    Inherits MarshalByRefObject
    Private m_MinutesToTime As Double 
    Private m_Timer As Timer

    ' The client will subscribe and unsubscribe to this event 
    Public Event TimerExpired As TimerExpiredEventHandler


    ' Default: Initialize the TimerService to 4 minutes, the time required 
    ' to brew coffee in a French Press. 
    Public Sub New() 
        MyClass.New(4.0)

    End Sub 'New 


    Public Sub New(ByVal minutes As Double) 
        Console.WriteLine("TimerService instantiated.")
        m_MinutesToTime = minutes
        m_Timer = New Timer()
        AddHandler m_Timer.Elapsed, AddressOf OnElapsed

    End Sub 'New 


    Public Property MinutesToTime() As Double  
        Get 
            Return m_MinutesToTime
        End Get 
        Set
            m_MinutesToTime = value
        End Set 
    End Property 


    Public Sub Start() 
        If Not m_Timer.Enabled Then 
            Dim interval As TimeSpan = TimeSpan.FromMinutes(m_MinutesToTime)
            m_Timer.Interval = interval.TotalMilliseconds
            m_Timer.Enabled = True 
        Else 
        End If 
     ' TODO: Raise an exception 
    End Sub 'Start


    Private Sub OnElapsed(ByVal [source] As Object, ByVal e As ElapsedEventArgs) 
        m_Timer.Enabled = False 

        ' Fire Event 
        If Not (TimerExpiredEvent Is Nothing) Then 
            ' Package String in TimerServiceEventArgs 
            Dim timerEventArgs As New TimerServiceEventArgs("TimerServiceEventArgs: Timer Expired.")
            Console.WriteLine("Firing TimerExpired Event")
            RaiseEvent TimerExpired(Me, timerEventArgs)
        End If 

    End Sub 'OnElapsed

    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)> _
    Public Overrides Function InitializeLifetimeService() As [Object] 
        Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)
        If lease.CurrentState = LeaseState.Initial Then
            lease.InitialLeaseTime = TimeSpan.FromMinutes(0.125)
            lease.SponsorshipTimeout = TimeSpan.FromMinutes(2)
            lease.RenewOnCallTime = TimeSpan.FromSeconds(2)
            Console.WriteLine("TimerService: InitializeLifetimeService")
        End If 
        Return lease

    End Function 'InitializeLifetimeService
End Class 'TimerService

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft