The following is in this article:
- What Concurrency is
- The difference between instancing and concurrency
- Instancing with Concurrency
- Attachment
- Summary
Concurrency in WCF
“ Several computations are executing simultaneously.”
I will just try to explain concurrency with simple day-to-day activity. Assume that you are going for a shopping to get some provisional goods for your home, if you find only one shopkeeper in that shop then obviously you must be in a queue to get your requirements done. Yes, the only shopkeeper serves the request one after the other. If the same shop has multiple shopkeepers then they will manage multiple buyers at the same time, the result will be that the requests are processed very quickly. If we relate the same activity with WCF then I could say that, by forming multiple threads in the server instance, more requests from the client proxy would be addressed in a concurrent fashion. We can use the concurrency feature in the following three ways:
- Single
- Multiple
- Reentrant
Single
A single request will be processed by a single thread on a server at any point of time. The client proxy sends the request to the server, it process the request and takes another request. One request will be processed on the server side at a time. Other requests need to wait until the request processed by the service is completed. The following is the declaration of Concurrency.Single:
Multiple
Multiple requests will be processed by multiple threads on the server at any point of time. The client proxy sends multiple requests to the server. Requests are processed by the server by spawning multiple threads on the server object. Doing this you will get more throughput here. The following is the declaration of Concurrency.Multiple.
Reentrant
The reentrant concurrency mode is nearly like the single concurrency mode. It is a single-threaded service instance that receives requests from the client proxy and it unlocks the thread only after the reentrant service object calls the other service or can also call a WCF client through call back. A reentrant service calls the other service or call back so other client requests would be allowed. The following is the declaration of Concurrency.Reentrant:
Difference between Instancing and Concurrency
- Instancing is about how the objects are created on the server, such as single or multiple objects are created on the server side
- Concurrency is about how the requests are handled by the WCF object, such as single or multiple threads are created on the server side to process the client requests
Instancing with the Concurrency
Though WCF provides many facilities to build a distributed system, to write efficient services the developer must think about the answer for the following questions:
- What is the frequency of the service instances that are to be created?
- How long should the created instance exist?
- How about the service going to manage the incoming client requests? What level of concurrency (Single or Multiple) is needed?
- Does your service require to maintain the state between calls?
Ok, let's discuss 9 combinations of instancing and concurrency.
Instance Mode -> Per Call and Concurrency -> Single
“Single thread for every client”
With the instance mode percall, new instances will be created for every service method call, with the concurrency mode Single, one thread will be created in the server to manage all the requests from one client.
In the preceding diagram you can find that a server instance will be created on every method call that the client does. The client will call a method by creating a new service instance, execute the service method and it releases the memory. One thread sitting on the server for each client manages the request in one after the other. Let's discuss the same concept with source code.
One is above the exposed service method that actually maintains the number of coffees selling the total sale made on coffee. For our demonstration purposes just the Thread Id is included to see how many threads are actually serving the client's request. Here is the client portion where it triggers the server method SellCoffee.
In the client I am just trying to sell 100 coffees at a moment. With this example we will see how the instances are created and how it manages the client requests.
If you see in the above snapshot, you can find that two client applications were launched. As I said earlier, the instance will be created on each and every server method call invocation. In our case the Instance number will always display the value 1 across the clients, because of the existing information will be lost on new instance creation. Since the concurrency mode is single, you can see in the above picture that each client request will be managed by a single thread (Thread Id 5 for Client 1) and (Thread Id 6 for Client 2). For further details please refer to the source code attached.
Instance Mode -> Per Call and Concurrency -> Multiple
“Multiple Threads for every client”




Melchior BusschersPosted May 7, 2020, 9:20 AM
I have issue when one client loose network connection iy does lock the other clients.