Using the server monitoring results, let's talk about the concurrent processing of WCF

Source: Internet
Author: User

Instancecontextmode indicates the number of instancecontext modes that WCF allows to generate instances for processing services (Contract classes) that can be called in incoming messages, the concurrency mode concurrencymode of WCF is for an instancecontext that encapsulates a service instance.

In short, instancecontextmode indicates how many service instance objects are generated, and concurrencymode indicates the concurrency control mode of each service instance object.

Instancecontextmode: There are three instance modes for the called Service (Contract class ).

Single Singleton mode: Each WCF Service creates an instancecontextmode object, which is created at the start of the service and destroyed at the completion of the service.
Persession session mode: each session generates an instancecontextmode object. The proxy's close method is called to destroy the object or the isterminating service is called to destroy the object;
Percall monotonic mode: each request generates an instancecontextmode object, which is destroyed after the call;

Concurrencymode: There are three concurrent behaviors of the WCF Service object (instancecontextmode ).

Single: An instancecontext object that encapsulates a service instance can only be used to process a single request at a certain time point. That is to say, multiple calls to the same service instance must be queued, it cannot continue until the last call is complete;
Reentrant: In the same mode as single, the instancecontext object can only be used to process a single request at a certain time point. However, the difference is that if a service operation involves a call out during execution, the instancecontext can be used to process call requests of other services ;. In single mode, when the method calls another service (callback is the service provided by the client), the method will be blocked until the called service is completed. If the method cannot be re-imported, a deadlock (deadlock) occurs because it cannot accept the returned message of the called Service (reply message ). The reentrant mode is used to solve the single deficiency. methods can be reimported to complete the processing.
Multiple: In this mode, an instancecontext can be used to process multiple service requests at the same time. Therefore, in the multiple concurrency mode, multiple concurrent requests for the same instancecontext can be processed in a timely manner. However, as it is a parallel processing method, service developers must handle state management and multi-threaded security issues during service operation execution.

The following uses a WCF server to monitorProgramTo describe the concurrent results produced by different combinations of three instancecontextmodes and three concurrencymodes.

1. instancecontextmode. Single Singleton mode. Each WCF Service generates only one instancecontextmode object for the Service (Contract class ).
· Concurrencymode. Single: The instancecontext object of the service instance can only be used to process a single request at a certain time point. The system automatically locks and has no concurrency issues.

WCF starts a thread 4 to receive the Add Method Request from the client 127.0.0.1: 7688. After processing, it receives the subtraction request from the client 127.0.0.1: 7689. WCF processes requests in serial queuing Mode

· Concurrencymode. reentrant: The instancecontext object of the service instance can only be used to process a single request at a certain time. This mode allows concurrent processing of a single re-entry thread and supports re-entry (callback) operations, this mode will take effect, and the thread returned from the callback will enter the queue end

WCF enable thread 4 to receive the add request from the client 127.0.0.1: 7582. After the request processing is complete, thread 4 continues callback processing. Colleagues enable thread 5 to receive requests from the client 127.0.0.1: 7851, this is the re-import concurrency mechanism of WCF.

· Concurrencymode. Multiple: The instancecontext of the service instance can be used to process multiple service requests at the same time. The system does not automatically lock the service. Service developers must handle status management and multi-threaded security issues during service operation execution.

Thread 4 receives the add request from the client 127.0.0.1: 7895, And the colleague opens thread 5 to receive the 127.0.0.1: 7894 request...

2. instancecontextmode. persession session mode each session generates an instancecontextmode object
· Concurrencymode. Single: An instancecontextmode object instance is created for each session. Only one thread operation instance is available for each session, and no concurrency is allowed. The system automatically locks and has no concurrency issues.


Thread 4 receives the add request from the client 127.0.0.1: 7904, and thread 5 receives the add request from 127.0.0.1: 7905... A single service object instance here is not concurrent. Although we see multiple threads, each session request actually generates only one service instance object, because the requests come from 127.0.0.1: 79004-127.0.0.1: 7908 five clients, so five service instance objects are generated. After thread 4 completes the add request to the client 127.0.0.1: 7904, it processes the subtraction request of 127.0.0.1: 7904. Therefore, this processing mode is still serialized within the service instance. Sessions are parallel.

· Concurrencymode. reentrant: each session creates an instancecontextmode object instance. Each session has only one thread operation instance at the same time, and the session can accept re-entry concurrency.

Thread 4 receives the add request from the client 127.0.0.1: 7863, and thread 5 receives the add request from 127.0.0.1: 7864... thread 4 starts to execute the callback request of 127.0.0.1: 7564, and starts thread 3 to execute the subtraction request of 727.0.0.1: 7863. This indicates that the WCF Service instance is re-routed, however, a new thread is enabled only after the callback request is sent to the same client.

· Concurrencymode. Multiple: An instancecontextmode object instance is created for each session. Multi-thread concurrency is allowed in each session, and the system does not automatically lock the lock, causing concurrency problems.

Thread 4 receives the add request from the client 127.0.0.1: 7938 and thread 5 receives the add request from 127.0.0.1: 7864. Thread 6 receives the substraction request from the client 127.0.0.1: 7938, requests (sessions) that come to the same customer are not queued up, but parallel.

3. instancecontextmode. percall: In monotonous mode, each request generates an instancecontextmode object.
· Concurrencymode. Single: each request generates an instancecontextmode object. Each request has only one thread operation instance, which does not allow concurrency. The system automatically locks the instance and has no concurrency issues.

Thread 4 receives the add request from the client 127.0.0.1: 8002, and thread 5 receives the request from the client 127.0.0.1: 8001... after thread 4 processes the add request of the client 127.0.0.1: 8002, enable thread 9 to receive the subtraction request of the same client 127.0.0.1: 8002, each request generates a new service instance object. Because there are multiple clients, multiple service instance objects are generated. This shows an external parallelism, but because the service instance objects are not allowed to be concurrently, although 127.0.0.1: 8002 of add requests and subtraction requests come to the same client, and there is still a queue.

· Concurrencymode. reentrant: each request generates an instancecontextmode object. Each request has only one thread operation instance, and the session can accept re-entry concurrency. When a single concurrent mode is used for a callback operation, a deadlock occurs (1. Call the server; 2. Call the callback client; 3. Return to the server; 1: Lock, it cannot be executed at 3, so it is deadlocked.) At this time, the reentrant concurrency mode should be used.

Thread 4 receives the add request from the client 127.0.0.1: 7884, and thread 5 receives the request from the client 127.0.0.1: 7883 because it is monotonous, the new thread 6 receives the subtraction request from the client 127.0.0.1: 7884. Thread 4 ends the add request from the client 127.0.0.1: 7884 and starts to process the Add callback request from the client 127.0.0.1: 7884, while processing the callback request, thread 6 is still processing the subtraction request of 127.0.0.1: 7884, which shows the concurrency of the callback.

· Concurrencymode. Multiple: each request generates an instancecontextmode object. Multi-thread concurrency is allowed in each request, and the system does not automatically lock the lock, causing concurrency problems.

Thread 4 receives the add request from the client 127.0.0.1: 3460 thread 5 receives the add request from the client 127.0.0.1: 3461... at the same time, thread 6 receives the subtraction request of the client 127.0.0.1: 3460, and does not queue the add request and subtraction request of the same client 127.0.0.1: 3460, Which is concurrent.

 

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.