HOW TO:建立共用物件及設定其大小和逾時限制
衍生自 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;
}
}
請參閱
參考
ObjectPoolingAttribute
System.EnterpriseServices Namespace
概念
Copyright © 2007 by Microsoft Corporation. All rights reserved.