My WCF service library is hosted as a Windows Service and is supposed to handle requests from multiple clients. One request that clients are going to make frequently is pretty resource intensive.
I have two doubts pertaining to above scenerio:
Thank you!
In your default scenario, the WCF service host (the thing hosting your service class) will create a new instance of your service class for each request that comes in, and lets that handle the request ("per-call" activation).
You can tweak the maximum number of those concurrently active service class instances using the serviceThrottling
behavior on your server.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ThrottledServiceBehavior">
<serviceThrottling
maxConcurrentCalls="25"
maxConcurrentSessions="25"
maxConcurrentInstances="25"/>
</behavior>
</serviceBehaviors>
</behaviors>
There's a really good explanation of the options of the service throttling behavior and its default values in Kenny Wolf's blog post here.
Also, setting of the InstanceContextMode
and ConcurrencyMode
on your service class (that implements the service contract) have a strong influence on how your service will handle concurrency and multiple requests.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,
ConcurrencyMode=ConcurrencyMode.Single)]
class YourServiceClass : IYourService
{
.....
}
InstanceContextMode
should be PerCall
(each calling request gets a new, separate instance) and then ConcurrencyMode
can be Single
(which is the easiest to develop).
InstanceContextMode
could also be PerSession
if you need a session-based approach (not very common), or Single
(your service class would a singleton - highly discouraged to use this, unless you absolutely, positively have to and know about all the quirks and problems with it!).
ConcurrencyMode
could also be Reentrant
(only relevant for duplex contracts and bindings) or Multiple
(multithreaded singleton service class - highly risky and difficult to develop!).
Marc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With