Share via


方法 :プールされるオブジェクトを作成しサイズ制限とタイムアウト制限を設定する

System.EnterpriseServices.ServicedComponent クラスから派生するクラスの場合、COM+ のオブジェクト プーリングを使用して、オブジェクトを最初からインスタンス化する際のオーバーヘッドを回避できます。代わりにオブジェクトは、アクティブ化されるとプールから取り出されます。詳細については、「オブジェクト プーリング」を参照してください。

プールされるオブジェクトを作成して、そのサイズ制限とタイムアウト制限を設定するには

  1. System.EnterpriseServices.ServicedComponent クラスから派生するクラスを定義し、ObjectPoolingAttribute 属性をそのクラスに適用します。たとえば、次のコードでは、TestObjectPooling という名前のクラスを定義し、そのクラスの MinPoolSizeMaxPoolSize、および CreationTimeout プロパティを設定します。

    <ObjectPooling(MinPoolSize := 2, MaxPoolSize := 5, _
    CreationTimeout := 20000)> _
    Public Class TestObjectPooling 
    Inherits ServicedComponent
    End Class 
    
    [ObjectPooling(Enabled=true, MinPoolSize=2, MaxPoolSize=5, CreationTimeout=20000)]
    public class TestObjectPooling : ServicedComponent
    {
    }
    
  2. System.EnterpriseServices.ServicedComponent クラスの ActivateDeactivate、および CanBePooled メソッドをオーバーライドします。

  3. 次の順序で、プールされたオブジェクトをクライアント アプリケーションでテストします。

    1. プールされたオブジェクト クラスのインスタンスを作成し、プールされたオブジェクトのメソッドを呼び出します。たとえば、次のコードでは、TestObjectPooling クラスのインスタンスを作成し、Perform メソッドを呼び出します。

      Public Class App
          Overloads Public Shared Sub Main(args() As String)
             Dim order As New TestObjectPooling()
                  order.Perform()
      
      
      public class App
      {
          public static int Main(string[] args)
          {
             TestObjectPooling order = new TestObjectPooling();
             order.Perform();
      
      
    2. DisposeObject メソッドを呼び出して、オブジェクトをプールに返します。

      ServicedComponent.DisposeObject (order)
      
      ServicedComponent.DisposeObject (order);
      

<ObjectPooling(MinPoolSize := 2, MaxPoolSize := 5, _
CreationTimeout := 20000)> _
Public Class TestObjectPooling 
Inherits ServicedComponent
      Public Sub Perform ()
            ' Method contents go here.
      End Sub 
      Protected Overrides Sub Activate()
            ' Called when removed from the pool.
      End Sub 
      Protected Overrides Sub Deactivate()
            ' Called before deactivating or placing back in pool.
      End Sub 
      Protected Overrides Function CanBePooled() As Boolean
            ' Called after Deactivate. Indicate your vote here.
            Return True
      End Function 
End Class 
[ObjectPooling(Enabled=true, MinPoolSize=2, MaxPoolSize=5, CreationTimeout=20000)]
public class TestObjectPooling : ServicedComponent
{
      public void Perform ()
      {
         // Method contents go here.
      }
      protected override void Activate()
      {
         // Called when removed from the pool.
      }
      protected override void Deactivate()
      {
         // Called before deactivating or placing back in pool.
      }
      protected override bool CanBePooled()
      {
         // Called after Deactivate. Indicate your vote here.
         return true;
      }
}
 

関連項目

参照

ObjectPoolingAttribute
System.EnterpriseServices Namespace

概念

利用可能な COM+ サービスの概要
オブジェクト プーリング

Footer image

Copyright © 2007 by Microsoft Corporation.All rights reserved.