WCF: How to add ServiceThrottlingBehavior to a WCF service?

I have the code below to return an instance of my WCF ServiceClient :

  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); 

Recently, I had problems with timeouts, so I decided to add throttling, for example:

  var throttlingBehaviour = new ServiceThrottlingBehavior () { MaxConcurrentCalls=500, MaxConcurrentInstances=500, MaxConcurrentSessions = 500 }; 

My question is: where in the code above should I add this throttlingBehaviour to my MusicRepo_DBAccess_ServiceClient instance?


From some examples that I found on the Internet, they do something like this:

 ServiceHost host = new ServiceHost(typeof(MyService)); ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 40, MaxConcurrentInstances = 20, MaxConcurrentSessions = 20, }; host.Description.Behaviors.Add(throttleBehavior); host.Open(); 

Please note that in the above code they use ServiceHost , while I am not, and then they open it (using Open() ), while I open the MusicRepo_DBAccess_ServiceClient ... and this is what got me confused.

+4
source share
3 answers

You can specify the behavior in the afaik configuration file, and the generated client will obey using the behavior.

Some configuration sections excluded for brevity

 <service behaviorConfiguration="throttleThis" /> <serviceBehaviors> <behavior name="throttleThis"> <serviceMetadata httpGetEnabled="True" /> <serviceThrottling maxConcurrentCalls="40" maxConcurrentInstances="20" maxConcurrentSessions="20"/> </behavior> </serviceBehaviors> 
+3
source

Can be done in code for those like me that are configured at runtime.

Vb version:

  Dim stb As New ServiceThrottlingBehavior stb.MaxConcurrentSessions = 100 stb.MaxConcurrentCalls = 100 stb.MaxConcurrentInstances = 100 ServiceHost.Description.Behaviors.Add(stb) 

C # version:

  ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior { MaxConcurrentSessions = 100, MaxConcurrentCalls = 100, MaxConcurrentInstances = 100 }; ServiceHost.Description.Behaviors.Add(stb); 
+17
source

Throttling - the behavior of the service side (server), not the client side

Arnon

+6
source

All Articles