System.EnterpriseServices.ServicedComponent クラスから派生したクラスの場合、COM+ オブジェクト プーリングを使用して、オブジェクトを最初からインスタンス化するオーバーヘッドを回避できます。オブジェクトは、アクティブになるとプールから取り出されるためです。詳細については、「オブジェクト プーリング」を参照してください。
プールされるオブジェクトを作成しサイズ制限とタイムアウト制限を設定するには
-
System.EnterpriseServices.ServicedComponent クラスから派生するクラスを定義し、ObjectPoolingAttribute 属性をそのクラスに適用します。たとえば、次のコードは、TestObjectPooling という名前のクラスを定義し、そのクラスの MinPoolSize プロパティ、MaxPoolSize プロパティ、および 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
{
}
-
System.EnterpriseServices.ServicedComponent クラスの Activate メソッド、Deactivate メソッド、および CanBePooled メソッドをオーバーライドします。
-
プールされたオブジェクトをクライアント アプリケーションでテストするには、次の手順を実行します。
-
プールされたオブジェクト クラスのインスタンスを作成し、プールされたオブジェクトのメソッドを呼び出します。たとえば、次のコードは、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();
-
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;
}
}
参照