How to: Create a Pooled Object and Set Its Size and Time-out Limits

[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

For a class that derives from the System.EnterpriseServices.ServicedComponent class, COM+ Object Pooling can be used to avoid the overhead of instantiating objects from scratch. Instead objects get pulled from a pool when activated. For more information, see Object Pooling.

To create a pooled object and set its size and time-out limits

  1. Define a class that derives from the System.EnterpriseServices.ServicedComponent class, and apply the ObjectPoolingAttribute attribute to the class. For example, the following code defines a class named TestObjectPooling and sets the MinPoolSize, MaxPoolSize and CreationTimeout properties for the class.

    <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. Override the Activate() , Deactivate(), and CanBePooled() methods of the System.EnterpriseServices.ServicedComponent class.

  3. Test the pooled object, in a client application:

    1. Create an instance of the pooled object class and call the methods on the pooled object. For example, the following code creates an instance of the TestObjectPooling class and calls a Perform method.

      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. Call the DisposeObject method to return the object to the pool.

      ServicedComponent.DisposeObject (order)
      

      ServicedComponent.DisposeObject (order);
      

[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;
      }
}
 
Show: