MarshalByRefObject.InitializeLifetimeService 方法

定義

警告

This Remoting API is not supported and throws PlatformNotSupportedException.

取得存留期服務物件,以控制這個執行個體的存留期原則。

C#
[System.Obsolete("This Remoting API is not supported and throws PlatformNotSupportedException.", DiagnosticId="SYSLIB0010", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual object InitializeLifetimeService ();
C#
public virtual object InitializeLifetimeService ();
C#
[System.Security.SecurityCritical]
public virtual object InitializeLifetimeService ();

傳回

Object

型別 ILease 的物件,用來控制這個執行個體的存留期原則。 如果存在目前存留期服務物件,則這是這個執行個體的目前存留期服務物件,否則為新的存留期服務物件,其初始化為 LeaseManagerPollTime 屬性 (Property) 的值。

屬性

例外狀況

立即呼叫端沒有基礎結構使用權限。

僅限 .NET Core 與 .NET 5+:在所有情況下。

範例

下列程式碼範例將示範如何建立租用。

C#
public class MyClass : MarshalByRefObject
{
  [SecurityPermissionAttribute(SecurityAction.Demand,
                               Flags=SecurityPermissionFlag.Infrastructure)]
  public override Object InitializeLifetimeService()
  {
    ILease lease = (ILease)base.InitializeLifetimeService();
    if (lease.CurrentState == LeaseState.Initial)
    {
         lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
         lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
          lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
    }
      return lease;
  }
}

備註

從 .NET 5 開始,這個方法會標示為過時。

如需有關存留期服務的詳細資訊,請參閱 LifetimeServices 類別。

適用於

產品 版本 (已淘汰)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 7 (5, 6)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1