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
ConcurrencyModesetting is dependent onInstanceContextModesetting.InstanceContextModedefines 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 serviceSo your description will be correct if you also use
InstanceContextMode.Singlebut default value isPerCallfor bindings not supporting sessions (basicHttpBinding, webHttpBinding, wsHttpBinding without security context or reliable session) orPerSessionfor bindings supporting sessions (netTcpBinding, netNamedPipeBinding, wsHttpBinding in default configuration using security context).ConcurrencyModedefines how can an instance of the service be used if multiple requests want to access it. For examplePerCallinstancing withMultipleconcurrency is the same asSingleconcurrency because each instance is used to serve exactly one request regardless the concurrency setting.