WCF: Come faccio ad aggiungere un comportamento ServiceThrottling per un servizio WCF?
-
16-09-2019 - |
Domanda
Ho il codice sotto per tornare indietro un'istanza della mia ServiceClient
servizio 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);
Ultimamente ho avuto qualche problema con i timeout e così ho deciso di aggiungere un comportamento di limitazione, come ad esempio:
var throttlingBehaviour = new ServiceThrottlingBehavior () {
MaxConcurrentCalls=500,
MaxConcurrentInstances=500,
MaxConcurrentSessions = 500
};
La mia domanda è, dove nel codice sopra dovrei aggiungere questo throttlingBehaviour
al mio esempio MusicRepo_DBAccess_ServiceClient
?
Da alcuni degli esempi che ho trovato sul web, che stanno facendo qualcosa di simile:
ServiceHost host = new ServiceHost(typeof(MyService));
ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
{
MaxConcurrentCalls = 40,
MaxConcurrentInstances = 20,
MaxConcurrentSessions = 20,
};
host.Description.Behaviors.Add(throttleBehavior);
host.Open();
Si noti che nel codice sopra stanno usando un ServiceHost
mentre io non sono, e sono quindi aprirlo (con Open()
), mentre apro l'istanza MusicRepo_DBAccess_ServiceClient
... e questo è ciò che mi ha confuso.
Soluzione
È possibile specificare il comportamento nel file di configurazione afaik, e il client generato sarà obbedire, usando i comportamenti.
Alcune sezioni di configurazione escluse per brevità
<service
behaviorConfiguration="throttleThis" />
<serviceBehaviors>
<behavior name="throttleThis">
<serviceMetadata httpGetEnabled="True" />
<serviceThrottling
maxConcurrentCalls="40"
maxConcurrentInstances="20"
maxConcurrentSessions="20"/>
</behavior>
</serviceBehaviors>
Altri suggerimenti
Può essere fatto in codice per chi, come me, che configurare in fase di esecuzione.
vb versione:
Dim stb As New ServiceThrottlingBehavior
stb.MaxConcurrentSessions = 100
stb.MaxConcurrentCalls = 100
stb.MaxConcurrentInstances = 100
ServiceHost.Description.Behaviors.Add(stb)
c # versione:
ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior {
MaxConcurrentSessions = 100,
MaxConcurrentCalls = 100,
MaxConcurrentInstances = 100
};
ServiceHost.Description.Behaviors.Add(stb);
La limitazione è un lato servizio (server) comportamento non lato client uno
Arnon