Synchronization in concurrency-Implementation of synchronization mechanism in the WCF concurrency System

Source: Internet
Author: User

In the nature of WCF concurrency, we talked about three different concurrency modes provided by WCF, so that developers can choose different concurrent processing policies based on specific situations. For these three concurrency modes, Multiple adopts parallel execution mode, while Single and Reentrant adopt serial execution mode. Serial execution is synchronous execution. In the WCF concurrency framework system, how is this synchronization mechanism implemented?

1. Synchronization implementation in Concurrency. Single Mode

In fact, the implementation of Concurrency. Single Mode in the framework of WCF Concurrency is very simple. Its essence is to lock InstanceContext. If you use the decompilation tool to view the definition of InstanceContext, you will find that the InstanceContext class defines a type named System. ObjectThisLockThis attribute is actually a reference to the attributes with the same name as the basic class CommunicationObject, as shown in the following code. WCF locks the ThisLock of InstanceContext to ensure synchronous access to InstanceContext.

   1: public sealed class InstanceContext : CommunicationObject, IExtensibleObject<InstanceContext>

   2: {

3: // other members

   4:     internal object ThisLock

   5:     {

   6:         get

   7:         {

   8:             return base.ThisLock;

   9:         }

  10:     }

  11: }

  12: public abstract class CommunicationObject : ICommunicationObject

  13: {

14: // other members

  15:     protected object ThisLock { get; }

  16: }

Specifically, the WCF server uses the instance context provider (InstanceContextProvider) to create a new or obtain an existing InstanceContext after processing the service call message request. Then, WCF distributes the request message to the InstanceContext for further processing. Before the processing operation is executed, if the corresponding service adopts the ConcurrencyMode mode. in Single mode, WCF will attempt to obtain the lock on the ThisLock of InstanceContext, or perform subsequent operations to lock the ThisLock of InstanceContext. This ensures that a Single InstanceContext object is always called in the ConcurrencyMode. Single concurrency mode in synchronous mode.

Ii. Synchronization implementation in Concurrency. Reentrant Mode

In ConcurrencyMode. Single concurrency mode, the InstanceContext is locked during the entire process from sending the request to the corresponding InstanceContext when the request is processed by the WCF server. If the callback to the client is involved in the service operation execution and the callback operation adopts the request/reply message exchange mode, after the response message returned from the client is received by the WCF server at runtime, the request message is distributed to the same InstanceContext again. The same mechanism is used for sending callback reply messages during runtime and for calling request messages of Common Services. You also need to lock the InstanceContext successfully. Obviously, a Deadlock occurs ).

Therefore, if you need to implement a callback to the client during service operation executionOne-way callbackOr set the concurrency mode of the Service to ConcurrencyMode. Reentrant or ConcurrencyMode. Multiple. Otherwise, suchFigure 1The InvalidOperationException is thrown during the callback operation. We can see from the exception message that the Chinese version of VS is really not flattering. If you want to understand the meaning of the exception message normally, you need to know that here the "email", "re-input", and "Multiple" are translated in turn for "Message", "Reentrant", and "Multiple.

Figure 1 exceptions caused by callback in Single Mode

If we really need to implement a callback based on the request/reply mode during service operations, there is no doubt that Concurrency is used. the Multiple Concurrency mode can solve the deadlock problem because of Concurrency. in the Multiple mode, the InstanceContext is locked. In Concurrency. Reentrant mode, how does the WCF Concurrency framework system solve this problem?

"Reentrant" means "re-import" (VS translates it into "re-input" rather than "re-input"), meaning that the Call Out is completed during service operations) you can return to the corresponding location to continue the execution. Like in Concurrency. Single mode, a lock is applied to call request messages before they are distributed to the corresponding InstanceContext during WCF runtime. However, before the callback is implemented, the lock on the InstanceContext will be removed, and the lock will be applied after the callback is returned.

For Concurrency. Reentrant, it is important to note that when the server performs a callback, the locks on the loading InstanceContext will be released, meaning otherService requests are distributed to the InstanceContext. When the callback returns, if InstanceContext is being used to process the request arriving during the callback process, it will wait even though it is the first to come, because the InstanceContext after the re-entry is locked. If the waiting time exceeds the set timeout time, the client will throw a TimeoutException exception.

Since the concurrency of WCF is for an InstanceContext that encapsulates a service instance, different concurrency behaviors are displayed in different instance context modes. In the next article, I will analyze the concurrency of WCF from the perspective of the specific instance context mode, so stay tuned.

Author: Artech
Source: http://artech.cnblogs.com
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.