WCF: ¿Cómo agrego un comportamiento ServiceThrottling a un servicio WCF?
-
16-09-2019 - |
Pregunta
Tengo el código de abajo para regresar de nuevo una instancia de mi ServiceClient
servicio WCF:
var readerQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 6000000,
MaxStringContentLength = 6000000,
MaxArrayLength = 6000000,
MaxBytesPerRead = 6000000,
MaxNameTableCharCount = 6000000
};
var throttlingBehaviour = new ServiceThrottlingBehavior(){MaxConcurrentCalls=500,MaxConcurrentInstances=500,MaxConcurrentSessions = 500};
binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};
dualBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None)
{MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};
endpointAddress = new EndpointAddress("http://localhost:28666/DBInteractionGateway.svc");
return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress);
Últimamente estaba teniendo algunos problemas con los tiempos de espera y por eso decidí añadir un comportamiento de estrangulación, como por ejemplo:
var throttlingBehaviour = new ServiceThrottlingBehavior () {
MaxConcurrentCalls=500,
MaxConcurrentInstances=500,
MaxConcurrentSessions = 500
};
Mi pregunta es, donde en el código anterior debería añadir este throttlingBehaviour
a mi ejemplo MusicRepo_DBAccess_ServiceClient
?
A partir de algunos de los ejemplos que encontré en la web, que están haciendo algo como esto:
ServiceHost host = new ServiceHost(typeof(MyService));
ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
{
MaxConcurrentCalls = 40,
MaxConcurrentInstances = 20,
MaxConcurrentSessions = 20,
};
host.Description.Behaviors.Add(throttleBehavior);
host.Open();
Tenga en cuenta que en el código anterior que están utilizando un ServiceHost
mientras que yo no soy, y que a continuación se abrirlo (con Open()
), mientras que abra la instancia MusicRepo_DBAccess_ServiceClient
... y esto es lo que me tiene confundido.
Solución
Puede especificar el comportamiento del archivo de configuración que yo sepa, y el cliente generada obedecerá, utilizando comportamientos.
Algunas secciones de configuración excluidos por razones de brevedad
<service
behaviorConfiguration="throttleThis" />
<serviceBehaviors>
<behavior name="throttleThis">
<serviceMetadata httpGetEnabled="True" />
<serviceThrottling
maxConcurrentCalls="40"
maxConcurrentInstances="20"
maxConcurrentSessions="20"/>
</behavior>
</serviceBehaviors>
Otros consejos
se puede hacer de código para los que, como yo, que configurar en tiempo de ejecución.
VB Versión:
Dim stb As New ServiceThrottlingBehavior
stb.MaxConcurrentSessions = 100
stb.MaxConcurrentCalls = 100
stb.MaxConcurrentInstances = 100
ServiceHost.Description.Behaviors.Add(stb)
C # versión:
ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior {
MaxConcurrentSessions = 100,
MaxConcurrentCalls = 100,
MaxConcurrentInstances = 100
};
ServiceHost.Description.Behaviors.Add(stb);
estrangulación es un lado de servicio (servidor) comportamiento no solo lado del cliente
Arnón