次の方法で共有


RemotingServices.GetLifetimeService メソッド

指定したオブジェクトの有効期間ポリシーを制御する、有効期間サービス オブジェクトを返します。

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

パラメータ

  • obj
    有効期間サービスを取得する対象となるオブジェクト。

戻り値

obj の有効期間を制御するオブジェクト。

例外

例外の種類 条件
SecurityException 直前の呼び出し元に、インフラストラクチャ アクセス許可がありません。

解説

既定の有効期間サービスの場合、返されるオブジェクトは ILease 型のオブジェクトです。 obj パラメータが null 参照 (Visual Basic では Nothing) の場合、このメソッドは null 参照 (Nothing) を返します。

使用例

[Visual Basic, C#, C++] GetLifetimeService メソッドを使用して、指定したオブジェクトの有効期間リースを取得する方法の例を次に示します。

 
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), "https://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), "https://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"https://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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

RemotingServices クラス | RemotingServices メンバ | System.Runtime.Remoting 名前空間