注意 应用程序域会影响对象池的行为。在 Microsoft Windows 2000 中,当应用程序激活设置为
Library 并且您有多个应用程序域时,将在默认应用程序域中创建所有缓冲池对象,并在多个客户端之间共享。在同样的情况下,当使用 Microsoft Windows XP 和 Windows Server 2003 时,每个应用程序域都有一个对象池。无论使用哪一种操作系统,当有多个应用程序域,且应用程序激活设置为服务器时,进程外客户端将使用默认应用程序域中的对象池。
下面的示例针对 TestObjectPooling 类设置大小和超时限制:
[Visual Basic]
<ObjectPooling(MinPoolSize := 2, MaxPoolSize := 5, _
CreationTimeout := 20000)> _
Public Class TestObjectPooling
Inherits ServicedComponent
Public Sub Perform ()
' Method contents go here.
End Sub
Public Overrides Sub Activate()
' Called when removed from the pool.
End Sub
Public Overrides Sub Deactivate()
' Called before deactivating or placing back in pool.
End Sub
Public Overrides Function CanBePooled() As Boolean
' Called after Deactivate. Indicate your vote here.
Return True
End Function
End Class
[C#]
[ObjectPooling(Enabled=true, MinPoolSize=2, MaxPoolSize=5, CreationTimeOut=20000)]
public class TestObjectPooling : ServicedComponent
{
public void Perform ()
{
// Method contents go here.
}
public override void Activate()
{
// Called when removed from the pool.
}
public override void Deactivate()
{
// Called before deactivating or placing back in pool.
}
public override bool CanBePooled()
{
// Called after Deactivate. Indicate your vote here.
return true;
}
}
客户端
[Visual Basic]
Public Class App
Overloads Public Shared Sub Main(args() As String)
Dim order As New TestObjectPooling()
order.Perform()
' To return the object to the object pool, use DisposeObject.
' This returns the object to the pool and allows it to be reused.
' If this call is not made, the garbage collector returns it to the pool
' in a non-deterministic fashion, which hinders performance
' of an application that depends on object pooling to conserve
' expensive resources.
ServicedComponent.DisposeObject (order)
End Sub
End Class
[C#]
public class App
{
public static int Main(string[] args)
{
TestObjectPooling order = new TestObjectPooling();
order.Perform();
/* To return the object to the object pool, use DisposeObject.
This returns the object to the pool and allows it to be reused.
If this call is not made, the garbage collector returns it to the pool
in a non-deterministic fashion, which hinders performance
of an application that depends on object pooling to conserve
expensive resources. */
ServicedComponent.DisposeObject (order);
}
}