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 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からILeaseを検索した結果を表示しています。
Weblioに収録されているすべての辞書からILeaseを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からILease を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ILease」の関連用語

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

   

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



ILeaseのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS