This article demo has been uploaded to GitHub: xinyuehtx / WCFdemo
This blog will explain how to set up concurrent service
Concurrent (Concurrency)
Concurrency control server context instance the InstanceContext
use of threads.
There are three concurrent value can be set Single
, Reentrant
,Mutiple
Which, `Mutiple is multi-threaded, meaning the service instance can allow multiple threads access
Single
And Reentrant
are single-threaded
Single
Single
Similar to the single-threaded synchronization, during a service call will block calls to other services
We make the following example. We set up a duplex communication services operating agreement is one-way operation agreement callback request is a reply (synchronous) of. In the callback method each call a new instance, and the callback method we did three seconds of sleep, analog communication delay. This time we call continuous service many times.
Results As shown, a deadlock occurs while waiting for the callback return
Since we returned from the callback, try to access the service object. But clients are single threaded synchronous, we are waiting for the callback method returns, resulting in a deadlock.
Now we try to operate a one-way agreement callback method changed
We see that the server has the execution order
A Reentrant (reentrant)
We just say that Single
may occur deadlock. And Reentrant
just to solve this problem.
Reentrant
Refers to invoke other services (or callback) In the method, the message may allow the subsequent re-entry (re-access) object
For the example of a deadlock, we only need to adjust the concurrent modeReentrant
We saw at the end when calling the callback service to start processing a message. Sequentially perform the subsequent return code message. Like async / await mode
Mutiple
Mutiple
It refers to the code allows multithreaded access.
We changed three examples of concurrencyMutiple
We can see the code output smoothly, but the order is chaos. Because multiple threads access.
summary
We studied the concurrent WCF service, its features are
Single
Maintaining single-threaded synchronization, but may deadlockReentract
Reentrant, the call to allow other services, similar to the async / awaitMutiple
Allows multithreaded access
Reference links:
- [WCF] concurrent service in the "re-entrant mode" - Click here to see Solitude - blog Park
- ConcurrencyMode Enum (System.ServiceModel) - Microsoft Docs
This article will be updated frequently, please read personal blog Original: https://xinyuehtx.github.io/ , in order to avoid misleading the old error of knowledge, and a better reading experience.
This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Huang Tengxiao (containing links: https://xinyuehtx.github.io/ ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please contact me .