MarshalByRefObject.InitializeLifetimeService 方法

定义

注意

This Remoting API is not supported and throws PlatformNotSupportedException.

获取生存期服务对象来控制此实例的生存期策略。

public:
 virtual System::Object ^ InitializeLifetimeService();
[System.Obsolete("This Remoting API is not supported and throws PlatformNotSupportedException.", DiagnosticId="SYSLIB0010", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual object InitializeLifetimeService ();
public virtual object InitializeLifetimeService ();
[System.Security.SecurityCritical]
public virtual object InitializeLifetimeService ();
[<System.Obsolete("This Remoting API is not supported and throws PlatformNotSupportedException.", DiagnosticId="SYSLIB0010", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
abstract member InitializeLifetimeService : unit -> obj
override this.InitializeLifetimeService : unit -> obj
abstract member InitializeLifetimeService : unit -> obj
override this.InitializeLifetimeService : unit -> obj
[<System.Security.SecurityCritical>]
abstract member InitializeLifetimeService : unit -> obj
override this.InitializeLifetimeService : unit -> obj
Public Overridable Function InitializeLifetimeService () As Object

返回

Object

ILease 类型的对象,用于控制此实例的生存期策略。 这是此实例当前的生存期服务对象(如果存在);否则为初始化为 LeaseManagerPollTime 属性的值的新生存期服务对象。

属性

例外

直接调用方没有基础结构权限。

仅限 .NET Core 和 .NET 5 及更高版本:在所有情况下。

示例

下面的代码示例演示如何创建租约。

public ref class MyClass: public MarshalByRefObject
{
public:
   
   [System::Security::Permissions::SecurityPermissionAttribute
   (System::Security::Permissions::SecurityAction::Demand,
   Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
   virtual Object^ InitializeLifetimeService() override
   {
      ILease^ lease = dynamic_cast<ILease^>(MarshalByRefObject::InitializeLifetimeService());
      if ( lease->CurrentState == LeaseState::Initial )
      {
         lease->InitialLeaseTime = TimeSpan::FromMinutes( 1 );
         lease->SponsorshipTimeout = TimeSpan::FromMinutes( 2 );
         lease->RenewOnCallTime = TimeSpan::FromSeconds( 2 );
      }

      return lease;
   }

};
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;
  }
}
Public Class LSClass
    Inherits MarshalByRefObject
    
    <SecurityPermissionAttribute(SecurityAction.Demand, _
                                 Flags:=SecurityPermissionFlag.Infrastructure)> _
    Public Overrides Function InitializeLifetimeService() As Object
        Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)
        If lease.CurrentState = LeaseState.Initial Then
            lease.InitialLeaseTime = TimeSpan.FromMinutes(1)
            lease.SponsorshipTimeout = TimeSpan.FromMinutes(2)
            lease.RenewOnCallTime = TimeSpan.FromSeconds(2)
        End If
        Return lease
    End Function


    Public Shared Sub Main()  
    ' The main thread processing is here.
    End Sub
End Class

注解

从 .NET 5 开始,此方法标记为已过时。

有关生存期服务的信息,请参阅 LifetimeServices 类。

适用于