MarshalByRefObject.InitializeLifetimeService 方法
定義
警告
This Remoting API is not supported and throws PlatformNotSupportedException.
取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.
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
傳回
型別 ILease 的物件,用來控制這個執行個體的存留期原則。An object of type ILease used to control the lifetime policy for this instance. 如果存在目前存留期服務物件,則這是這個執行個體的目前存留期服務物件,否則為新的存留期服務物件,其初始化為 LeaseManagerPollTime 屬性 (Property) 的值。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
{
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.0 開始,此方法已標示為過時。This method is marked obsolete starting in .NET 5.0.
如需有關存留期服務的詳細資訊,請參閱 LifetimeServices 類別。For more information about lifetime services, see the LifetimeServices class.