Friday, February 04, 2011

I have just found myself answering essentially the same question 4 times on the MSDN WCF Forum about how instances, threading and throttling interact in WCF. So to save myself some typing I will walk through the relationships here and then I can reference this post in questions.

Instancing
WCF has 3 built in instancing models: PerCall, PerSession and Single. They are set on the InstanceContextMode on the ServiceBehavior attribute on the service implementation. They relate to how many instances of the service implementation class get used when requests come in, and work as follows:

  • Single – one instance of the implementation class is used for all requests
  • PerCall – every request gets its own instance of the implementation class
  • PerSession – this is the slightly odd one as it means every session gets its own instance. In practical terms it means that for Session supporting bindings: NetTcpBinding, WSHttpBinding, NetNamedPipeBinding, etc, every proxy gets an instance of the service. For bindings that do not support session: BasicHttpBinding, WebHttpBinding, we get the same effect as PerCall. To add to the confusion, this setting is the default

Concurrency
By default WCF assumes you do not understand multithreading. Therefore, it only allows one thread at a time into an instance of the service implementation class unless you tell it otherwise. You can control this behavior using the ConcurrencyMode on the ServiceBehavior; it has 3 values:

  • Single – this is the default and only one thread can get into an instance at a time
  • Multiple – any thread can enter an instance at any time
  • Reentrant – only makes a difference in duplex services but means that an inbound request can be received from the component you are currently making a request to (sounds a bit vague but in duplex the role of service and client are somewhat arbitrary)

Interaction
Now these two concepts are different but have some level of interaction.

If you set InstanceContextMode to Single and ConcurrencyMode to Single then your service will process exactly one request at a time. If you set InstanceContextMode to Single and ConcurrencyMode to Multiple then your service processes many requests but you are responsible for ensuring your code is threadsafe.

If you set InstanceContextMode to PerCall then ConcurrencyMode Single and Multiple behave the same as each request gets its own instance

For PerSession ConcurrencyMode Multiple is only required if you want to support a client sending multiple requests through the same proxy from multiple threads concurrently

Threading
Unless you turn on ASP.NET Compatibility, WCF calls are processed on IO threads in the system threadpool. There is no thread affinity so any of these threads could process a request. The number of threads being used will grow until the throughput of the service matches the number of concurrent requests (assuming the server machine has the resources to match the number of concurrent requests). Although the number of IO threads is capped at 1000 by default, if you hit this many then unless you are running on some big iron hardware you probably have problems in your architecture.

Throttling
Throttling is there to ensure your service is not swamped in terms of resources. There are three throttles in place:

  • MaxConcurrentCalls – the number of concurrent calls that can be made – under .NET 4 defaults to 16 x number of cores
  • MaxConcurrentSessions – the number of concurrent sessions that can be in in flight – under .NET 4 defaults to 100 x number of cores
  • MaxConcurrentObjects – the number of service implementation objects that are in use – defaults to the sum of MaxConcurrentCalls + MaxConcurrentSessions

In reality, depending on whether you are using sessions or not, the session or call throttle will affect your service the most. The object one will only affect your service if you set it lower than the others or you do something unsual and handle the mapping of requests to objects yourself using a custom IInstanceContextProvider

You can control the throttle values using the serviceThrottling service behavior which you set in the config file or in code

 | 
Friday, February 04, 2011 8:50:40 PM (GMT Standard Time, UTC+00:00)  #    Comments [4]Trackback
Friday, August 12, 2011 2:27:03 AM (GMT Daylight Time, UTC+01:00)
This is a good article and offer some helpful information for me,thank you!
Thursday, September 15, 2011 12:39:03 AM (GMT Daylight Time, UTC+01:00)
This post is creativethere are a variety of new ideait provides me with inspiration,Thank you!
Saturday, September 17, 2011 9:00:59 AM (GMT Daylight Time, UTC+01:00)
Non era una fuga veloce, in quanto il veicolo in esclusiva viaggio circa 15 km / h, comunque acquisito tuoi ragazzi desiderato può rovinare, questo veicolo a motore possono avere indicazioni più appiattita di più, ha detto Clark. "Si può andare. ", <a href="http://www.bootsebay.net/boots-uomo-wholesale-35.html">stivali uomo</a> , come era stato, il rullo compressore viaggiato per mezzo di Vancouver o pavimentazione forse Heywood per aiutare Southgate Avenue, dopo di che tecnica Arbutus nel prato più è stato ancora posto all'interno del parco giochi per bambini, ha detto Clark. I segreti sono stati effettivamente ancora a sinistra considerando il veicolo. Victoria polizia ha cercato per voi di cogliere i sospetti, che potrebbe essere visto all'interno della comunità è sufficiente da parte dei funzionari che hanno aderito questa arena. Una sorta di unità di animali ha finito per essere introdotti, ma i sospetti ottenuto da parte., <a href="http://www.bootsebay.net/boots-uomo-wholesale-35.html">stivali uomo</a> , il rullo compressore era stata acquistata con il giorno Sabato e città è oggi di tornare con una struttura di sito web per tutta Fairfield.
Friday, December 02, 2011 12:51:41 AM (GMT Standard Time, UTC+00:00)
<a href="http://www.usashoesoutlet.com/">nike shoes outlet</a> Remember, once, the girl a drink, it is a girl of a friend's birthday, so the girl a drink, the boy didn't know. If know
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):