Cómo crear un objeto agrupado y establecer sus límites de tamaño y tiempo de espera
Para una clase que se derive de la clase System.EnterpriseServices.ServicedComponent, se puede utilizar la agrupación de objetos COM+ para evitar la sobrecarga de crear instancias de objetos desde cero. En su lugar se extraen los objetos de una agrupación cuando se activa. Para obtener más información, vea Agrupación de objetos.
Para crear un objeto agrupado y establecer sus límites de tamaño y tiempo de espera
Defina una clase que se derive de la clase System.EnterpriseServices.ServicedComponent y aplique el atributo ObjectPoolingAttribute a la clase. Por ejemplo, el código siguiente define una clase denominada
TestObjectPooling
y establece las propiedades MinPoolSize, MaxPoolSize yCreationTimeout de la clase.<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 { }
Reemplace los métodos Activate , Deactivate yCanBePooled de la clase System.EnterpriseServices.ServicedComponent.
Pruebe el objeto agrupado en una aplicación cliente:
Cree una instancia de la clase de objeto agrupado y llame a los métodos del objeto agrupado. Por ejemplo, en el siguiente código se crea una instancia de la clase
TestObjectPooling
y llama a un métodoPerform
.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();
Llame al método DisposeObject para devolver el objeto a la agrupación.
ServicedComponent.DisposeObject (order)
ServicedComponent.DisposeObject (order);
Ejemplo
<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;
}
}
Consulte también
Referencia
ObjectPoolingAttribute
System.EnterpriseServices Namespace
Conceptos
Resumen de los servicios COM+ disponibles
Agrupación de objetos
Copyright © 2007 Microsoft Corporation. Reservados todos los derechos.