How to Implement concurrency for a single-instance WCF Service

Source: Internet
Author: User

See artech'sArticle, There are unexpected gains, and the gains are not small. If you want to use it more easily, it is also relatively simple, because many attributes have default values. However, Web services are often compared in some aspects, which are different in many aspects. Let's talk about using WCF to support concurrency.

To understand the concurrency of WCF, we need to understand three very important things:
1: instance context, which encapsulates service instances and is an important part of instance service management.
2: a service instance is a real service instance.
3: Service proxy, a bridge between the client and the real service.

Relationship between the service instance and the corresponding instance context:
To develop a service, we first need to determine its service instance. WCF provides three different service instances, and these three service instances have different context for their instances. They are:
1: monotonous service, that is, the request/response mode. Whether the same client or different clients call services, a new service instance context is created in a unified manner. For a monotonous instance, the returned instance context is null, that is, the instance context does not need to be maintained. The instance context is meaningless for monotonous services. For some concurrent requests, monotonous services can be promptly processed.
2: The Session Service maintains the same service status for the same client. Different clients do not share the same instance context. This mode is between monotonous services and Singleton services, like a singleton service, it also requires additional maintenance of the Instance context.
3: single-instance service. Only one service instance context is created for all clients. This means that the instance context needs to be maintained after each client call.

The relationships between the three instance modes are as follows:

 

Impact on concurrency in three instance modes:
1: monotonous service. In a monotonous service, when a request arrives at the server, a new service instance and instance context are created, therefore, there are no concurrent calls of multiple requests to the same service instance. Therefore, when there are concurrent requests, they can be responded in a timely manner. This also indicates that concurencymode. mutiple is meaningless for monotonous services.
2: For a singleton service, because of the uniqueness of the service instance, each request arrives at the server and calls the same service instance. In addition, the context consistency of the Instance needs to be maintained, that is to say, when multiple requests are concurrently called, a request queue is formed for serial execution, which greatly affects the concurrency of the system. However, we can solve this problem by setting the concurrency mode of WCF, and concurrency mode, set this attribute to concurencymode. mutiple, it should be noted that we need to manually control a series of problems caused by multithreading.
3: The preceding two methods of session service are unified.

Problem: If we observe the above regulations, will the service actually start the concurrency mode? After reading the artech article, I also downloaded his demo. There are two problems:
First, there is indeed no real startup concurrency mode. That's because usesynchronizationcontext is not set. This attribute is set to true by default, that is, the synchronization instance context. If it is set to false, the system will actually start the concurrency mode. Program .
Second: it is also a problem caused by artech's demo. In the artech's WCF analysis book, how to implement concurrency for Singleton services is mentioned, you need to set the concurrency mode attribute to concurencymode. mutiple, But I have tested it and cannot really start the concurrency. The program is still running in serial mode. You only need to set usesynchronizationcontext to false. However, if the context is not synchronized, the meaning of setting the context to a singleton service does not exist. I cannot get the answer at the moment, please help artech to find out the reasons. Thank you first.

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.