MarshalByRefObject.InitializeLifetimeService 方法


获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

 virtual System::Object ^ InitializeLifetimeService();
public virtual object InitializeLifetimeService ();
public virtual object InitializeLifetimeService ();
abstract member InitializeLifetimeService : unit -> obj
override this.InitializeLifetimeService : unit -> obj
abstract member InitializeLifetimeService : unit -> obj
override this.InitializeLifetimeService : unit -> obj
Public Overridable Function InitializeLifetimeService () As Object



ILease 类型的对象,用于控制此实例的生存期策略。An object of type ILease used to control the lifetime policy for this instance. 这是此实例当前的生存期服务对象(如果存在);否则为初始化为 LeaseManagerPollTime 属性的值的新生存期服务对象。This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LeaseManagerPollTime property.



直接调用方没有基础结构权限。The immediate caller does not have infrastructure permission.

仅限 .NET Core 和 .NET 5 及更高版本:在所有情况下。.NET Core and .NET 5+ only: In all cases.


下面的代码示例演示如何创建租约。The following code example demonstrates creating a lease.

public ref class MyClass: public MarshalByRefObject
   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
  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.0 开始,此方法标记为已过时。This method is marked obsolete starting in .NET 5.0.

有关生存期服务的详细信息,请参阅 LifetimeServices 类。For more information about lifetime services, see the LifetimeServices class.