wcf entry (8) - Concurrency Control

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 InstanceContextuse 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

SingleAnd Reentrantare single-threaded

Here Insert Picture Description

Single

SingleSimilar 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.

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Results As shown, a deadlock occurs while waiting for the callback return
Here Insert Picture Description

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

Here Insert Picture Description

We see that the server has the execution order

A Reentrant (reentrant)

We just say that Singlemay occur deadlock. And Reentrantjust to solve this problem.

ReentrantRefers 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

Here Insert Picture Description

Here Insert Picture Description

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

MutipleIt refers to the code allows multithreaded access.

We changed three examples of concurrencyMutiple
Here Insert Picture Description

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

  • SingleMaintaining single-threaded synchronization, but may deadlock
  • ReentractReentrant, the call to allow other services, similar to the async / await
  • MutipleAllows multithreaded access

Reference links:


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.

Creative Commons License 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 .

Published 54 original articles · won praise 0 · Views 2427

Guess you like

Origin blog.csdn.net/htxhtx123/article/details/104231410