This documentation is archived and is not being maintained.

RemotingServices.GetLifetimeService Method

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

[Visual Basic]
Public Shared Function GetLifetimeService( _
   ByVal obj As MarshalByRefObject _
) As Object
[C#]
public static object GetLifetimeService(
 MarshalByRefObject obj
);
[C++]
public: static Object* GetLifetimeService(
 MarshalByRefObject* obj
);
[JScript]
public static function GetLifetimeService(
   obj : MarshalByRefObject
) : Object;

Parameters

obj
The object to obtain lifetime service for.

Return Value

The object that controls the lifetime of obj.

Exceptions

Exception Type Condition
SecurityException The immediate caller does not have infrastructure permission.

Remarks

For the default lifetime service the returned object will be an object of type ILease. If the obj parameter is a null reference (Nothing in Visual Basic), the method returns a null reference (Nothing).

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetLifetimeService method to get a lifetime lease for the specified object.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
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 TimerSample


Namespace GroupCoffeeTimer
   Public Class TimerClient
      Inherits MarshalByRefObject
      Implements ISponsor
      
      Public Shared Sub Main()
         Dim myClient As New TimerClient()
      End Sub 'Main
      
      
      Public Sub New()
         ' Registers the HTTP Channel so that this client can receive
         ' events from the remote service.
         ChannelServices.RegisterChannel(New HttpChannel(0))
         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
      
      
      Public Sub OnTimerExpired([source] As Object, e As TimerServiceEventArgs)
         Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message)
      End Sub 'OnTimerExpired
      
      
      Public Function Renewal(lease As ILease) As TimeSpan Implements ISponsor.Renewal
         Console.WriteLine("TimerClient: Renewal called.")
         Return TimeSpan.FromMinutes(0.5)
      End Function 'Renewal
   End Class 'TimerClient
End Namespace 'GroupCoffeeTimer

[C#] 
using System;
using System.Net.Sockets;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using TimerSample;

namespace GroupCoffeeTimer {
    public class TimerClient : MarshalByRefObject, ISponsor {
        public static void Main() {
            TimerClient myClient = new TimerClient();
        }

        public TimerClient() {
            // Registers the HTTP Channel so that this client can receive
            // events from the remote service.
            ChannelServices.RegisterChannel(new HttpChannel(0));
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(TimerService), "http://localhost:9000/MyService/TimerService.soap");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);
            
            TimerService groupTimer = new TimerService();
            groupTimer.MinutesToTime = 4.0; 

            // Registers this client as a lease sponsor so that it can
            // prevent the expiration of the TimerService.
            ILease leaseObject = (ILease)RemotingServices.GetLifetimeService(groupTimer);
            leaseObject.Register(this);
            
            // Subscribes to the event so that the client can receive notifications from the server.
            groupTimer.TimerExpired += new TimerExpiredEventHandler(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();
        }
        
        public void OnTimerExpired (object source, TimerServiceEventArgs e) {
            Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message);
        }

        public TimeSpan Renewal(ILease lease) {
            Console.WriteLine("TimerClient: Renewal called.");
            return TimeSpan.FromMinutes(0.5);
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.runtime.remoting.dll>

using namespace System;
using namespace System::Net::Sockets;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
using namespace System::Runtime::Remoting::Messaging;
using namespace System::Runtime::Remoting::Lifetime;
using namespace TimerSample;

namespace GroupCoffeeTimer 
{
public __gc class TimerClient : public MarshalByRefObject, public ISponsor
{

public:
    TimerClient() 
    {
        // Registers the HTTP Channel so that this client can receive
        // events from the remote service.
        ChannelServices::RegisterChannel(new HttpChannel(0));
        WellKnownClientTypeEntry* remoteType = new WellKnownClientTypeEntry(__typeof(TimerService), S"http://localhost:9000/MyService/TimerService.soap");
        RemotingConfiguration::RegisterWellKnownClientType(remoteType);

        TimerService* groupTimer = new TimerService();
        groupTimer->MinutesToTime = 4.0; 

        // Registers this client as a lease sponsor so that it can
        // prevent the expiration of the TimerService.
        ILease* leaseObject = dynamic_cast<ILease*>(RemotingServices::GetLifetimeService(groupTimer));
        leaseObject->Register(this);

        // Subscribes to the event so that the client can receive notifications from the server.
        groupTimer->TimerExpired += new TimerExpiredEventHandler(this, OnTimerExpired);
        Console::WriteLine(S"Connected to TimerExpired event");

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

public:
    void OnTimerExpired(Object*, TimerServiceEventArgs* e) 
    {
        Console::WriteLine(S"TimerHelper::OnTimerExpired: {0}", e->Message);
    }

public:
    TimeSpan Renewal(ILease*) 
    {
        Console::WriteLine(S"TimerClient: Renewal called.");
        return TimeSpan::FromMinutes(0.5);
    }
};
}

int main() 
{
    using namespace GroupCoffeeTimer;
    new TimerClient();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

RemotingServices Class | RemotingServices Members | System.Runtime.Remoting Namespace

Show: