如何:重写 InitializeLifetimeService 接口

此代码示例演示对 InitializeLifetimeService 接口进行标准重写。

示例

Public Class MyLifetimeControlObject
  Inherits MarshalByRefObject
  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 'InitializeLifetimeService
End Class 'MyLifetimeControlObject
public class MyLifetimeControlObject: 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;
   }
}

编译代码

此示例需要:

  • System.Runtime.Remoting.Lifetime 命名空间的引用。

请参见

概念

初始化租约

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。