Understanding WCF ServiceBehaviorProperty ConcurrencyMode
I would like to understand the ServiceBehavior.ConcurrencyMode
property.
Consider the following code on service side:
[ServiceContract]
public interface IMySercice {
[OperationContract]
int mycall(int a);
}
/*** HERE I WILL GENERATE Two variants of this code ***/
[ServiceBehaviour(InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
// Constructors...
// implementations of interface
public int mycall(int a) { ... }
}
class Program {
static void Main(string[] args) {
MyServiceMyS = new MyService(...); /* Creating service instance */
Uri MyUri = new Uri("http://services.mycompany.com/services/"); /* Base address for my hosted service */
using (ServiceHost host = new ServiceHost(MyS)) { /* Defining service host, configuration file contains info regarding endpoints, not shown here for clarity */
host.Start();
host.Stop();
}
}
}
Now consider that I would like to call this service, please consider 10 machines in the web that may call my service.
At a certain point, it happens that these 10 machines ALL MAKE 10 simultaneous requests for int mycall(int a)
.
I would like to examine these scenarios:
SCENARIO 1
...
/*** VARIANT 1 ***/
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
...
}
...
SCENARIO 2
...
/*** VARIANT 2 ***/
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContext = InstanceContextMode.Single)]
public class MyService : IMyService {
...
}
...
So, 10 simultaneous calls arrive... what does it happen in both cases? Please tell开发者_运维技巧 me whether I am right or wrong:
In Scenario 1, single threaded, these 10 calls are queued and executed one at a time. My service (THE SAME INSTANCE OF MY SERVICE)'s method will be called ten times in sequence. In Scenario 2, multiple threaded, WCF will cause 10 threads to simultaneously call my service method.
Is it true what I said? Thanks
In both scenarios all 10 clients will be served in parallel by 10 different threads. The reason is that ConcurrencyMode
setting is dependent on InstanceContextMode
setting. InstanceContextMode
defines how WCF engine servers client requests - how is new instance of the service created if request arrives:
PerCall
- each request is served by a new instance of the servicePerSession
- all request from single proxy instance are served by the same service instanceSingle
- per host singleton - all request are served by the same instance of the service
So your description will be correct if you also use InstanceContextMode.Single
but default value is PerCall
for bindings not supporting sessions (basicHttpBinding, webHttpBinding, wsHttpBinding without security context or reliable session) or PerSession
for bindings supporting sessions (netTcpBinding, netNamedPipeBinding, wsHttpBinding in default configuration using security context).
ConcurrencyMode
defines how can an instance of the service be used if multiple requests want to access it. For example PerCall
instancing with Multiple
concurrency is the same as Single
concurrency because each instance is used to serve exactly one request regardless the concurrency setting.
Yes, using InstanceContextMode.Single
what you've described should be the behavior you should see. Beyond 10 simultaneous calls, you would actually still get the same behavior, because you would hit the default throttling limit of 10 for MaxConcurrentSessions
on your service's ServiceThrottlingBehavior
(a "session" as far as the throttling behavior is concerned is the total number of channels that can be open at a time).
精彩评论