ILeaseとは? わかりやすく解説

ILease インターフェイス

リモート処理有効期間サービスによって使用される有効期間リース オブジェクトを定義します。

名前空間: System.Runtime.Remoting.Lifetime
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Interface ILease
[ComVisibleAttribute(true)] 
public interface ILease
[ComVisibleAttribute(true)] 
public interface class ILease
/** @attribute ComVisibleAttribute(true) */ 
public interface ILease
ComVisibleAttribute(true) 
public interface ILease
解説解説

配布されたガベージ コレクションは、サーバー アプリケーションをいつ削除できるかを制御します。従来、配布されたガベージ コレクションは、制御のために参照カウントと ping を使用していました。この方法は、オブジェクトごとのクライアント数が少ない場合には機能しますが、千単位のクライアント数の場合にはうまく機能しません。有効期間サービスは、従来の配布されたガベージ コレクタの機能を実行できます。さらに、クライアント数が増加した場合にも対応できます。

有効期間サービスは、リモートの各アクティブ化オブジェクトにリースを関連付けます。リースの有効期限が切れると、オブジェクトが削除されます。リースを使用すると、オブジェクトに無期限の有効期間を指定できます。

各 AppDomain は、ドメイン内のリースを管理するリース マネージャを格納します。リース マネージャは、有効期限が切れていないか定期的にリースをチェックします。リースの有効期限が切れていた場合は、そのリースへの参照を削除してキャンセルするか、1 つ以上のリースのスポンサを呼び出してそのリースを更新できます。

リースは、ポリシーを決定するプロパティと、リース期間を更新するメソッドを格納します。また、ILease インターフェイスを公開します。

使用例使用例

クライアント、サーバー、およびクライアントとサーバーによって共有されるライブラリの 3 つのアセンブリで構成される例を次に示します。最初にライブラリをコンパイルし、次にクライアントとサーバーをコンパイルします。Visual Basic ファイルをコンパイルするコマンドを次に示します。C# ファイルをコンパイルする場合は、csc コマンドと .cs ファイル拡張子を使用してください。次に示すファイル名は、単に推奨例です。

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

必ずしもすべての Visual Basic ファイルまたは C# ファイルを使用する必要はありません。コンパイルすると、アセンブリはソース言語に関係なく連携して動作します。この例を実行するには、2 つのコマンド ウィンドウを開きます。最初にサーバーを実行し、次にクライアントを実行します。クライアントとサーバーを別のフォルダに配置している場合は、各フォルダに共有ライブラリのコピーを配置する必要があります。

次のコードは、共有ライブラリの例です。

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
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;
      }
   }
}
#using <system.dll>
#using <system.runtime.remoting.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Lifetime;

namespace RemotingSamples
{
   public ref class HelloService: public
 MarshalByRefObject
   {
   public:
      String^ HelloMethod( String^ name )
      {
         Console::WriteLine( "Hello {0}", name );
         return "Hello {0}",name;
      }

      [System::Security::Permissions::SecurityPermissionAttribute
      (System::Security::Permissions::SecurityAction::LinkDemand, 
      Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
      virtual Object^ InitializeLifetimeService() override
      {
         ILease^ baseLease = dynamic_cast<ILease^>(MarshalByRefObject::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;
      }

   };

}

次のコードは、クライアント アセンブリの例です。

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
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);
      }
   }
}
#using <system.dll>
#using <system.runtime.remoting.dll>
#using <ILease_Share.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
using namespace System::Runtime::Remoting::Lifetime;
using namespace RemotingSamples;

int main()
{
   // Register the channel.
   TcpChannel^ myChannel = gcnew TcpChannel;
   ChannelServices::RegisterChannel( myChannel );
   RemotingConfiguration::RegisterActivatedClientType( HelloService::typeid, "Tcp://localhost:8085"
 );
   TimeSpan myTimeSpan = TimeSpan::FromMinutes( 10 );

   // Create a remote object.
   HelloService ^ myService = gcnew HelloService;
   ILease^ myLease;
   myLease = dynamic_cast<ILease^>(RemotingServices::GetLifetimeService( myService
 ));
   if ( myLease == nullptr )
   {
      Console::WriteLine( "Cannot lease." );
      return  -1;
   }

   Console::WriteLine( "Initial lease time is {0}", myLease->InitialLeaseTime
 );
   Console::WriteLine( "Current lease time is {0}", myLease->CurrentLeaseTime
 );
   Console::WriteLine( "Renew on call time is {0}", myLease->RenewOnCallTime
 );
   Console::WriteLine( "Sponsorship timeout is {0}", myLease->SponsorshipTimeout
 );
   Console::WriteLine( "Current lease state is {0}", myLease->CurrentState
 );

   // Register with a sponser.
   ClientSponsor^ mySponsor = gcnew 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 {0}", myLease->CurrentLeaseTime
 );

   // Renew the lease time.
   myLease->Renew( myTimeSpan );
   Console::WriteLine( "Current lease time after renewal is {0}", myLease->CurrentLeaseTime
 );

   // Call the Remote method.
   Console::WriteLine( "Remote method output is {0}", 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 {0}", myLease->CurrentLeaseTime
 );

   // Call the Remote method.
   Console::WriteLine( "Remote method output is {0}", myService->HelloMethod(
 "Microsoft" ) );
   myLease->Unregister( mySponsor );
}

次のコードは、サーバー アセンブリの例です。

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
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();
      }
   }
}
#using <system.dll>
#using <system.runtime.remoting.dll>
#using <ILease_Share.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
using namespace System::Runtime::Remoting::Lifetime;
using namespace RemotingSamples;

int main()
{
   TcpChannel^ myChannel = gcnew TcpChannel( 8085 );
   ChannelServices::RegisterChannel( myChannel );
   RemotingConfiguration::RegisterActivatedServiceType( HelloService::typeid );
   Console::WriteLine( "Server started." );
   Console::WriteLine( "Hit enter to terminate..." );
   Console::Read();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ILease メンバ
System.Runtime.Remoting.Lifetime 名前空間

ILease プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ CurrentLeaseTime リースの残り時間を取得します。
パブリック プロパティ CurrentState リースの現在の LeaseState を取得します。
パブリック プロパティ InitialLeaseTime リースの初期時間を取得または設定します。
パブリック プロパティ RenewOnCallTime リモート オブジェクトに対する呼び出しによって、CurrentLeaseTime が更新されるのにかかる時間を取得または設定します。
パブリック プロパティ SponsorshipTimeout スポンサがリースの更新時間を返すまで待機する時間を取得または設定します。
参照参照

関連項目

ILease インターフェイス
System.Runtime.Remoting.Lifetime 名前空間

ILease メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド Register オーバーロードされます。 リースのスポンサを登録します。
パブリック メソッド Renew 指定した時間間隔でリースを更新します。
パブリック メソッド Unregister スポンサ リストからスポンサを削除します。
参照参照

関連項目

ILease インターフェイス
System.Runtime.Remoting.Lifetime 名前空間

ILease メンバ

リモート処理有効期間サービスによって使用される有効期間リース オブジェクトを定義します。

ILease データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ CurrentLeaseTime リースの残り時間を取得します。
パブリック プロパティ CurrentState リースの現在の LeaseState を取得します。
パブリック プロパティ InitialLeaseTime リースの初期時間を取得または設定します。
パブリック プロパティ RenewOnCallTime リモート オブジェクトに対する呼び出しによって、CurrentLeaseTime が更新されるのにかかる時間を取得または設定します。
パブリック プロパティ SponsorshipTimeout スポンサがリースの更新時間を返すまで待機する時間を取得または設定します。
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド Register オーバーロードされます。 リースのスポンサを登録します。
パブリック メソッド Renew 指定した時間間隔でリースを更新します。
パブリック メソッド Unregister スポンサ リストからスポンサを削除します。
参照参照

関連項目

ILease インターフェイス
System.Runtime.Remoting.Lifetime 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ILease」の関連用語

ILeaseのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ILeaseのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS