By exposing a service so that it is consumed, we must look to the possibility of this service being accessed simultaneously. This occurs when multiple requests ( threads ) try to access the same resource at the same time.The possibility of simultaneous accesses may occur depending on the instance management chosen for the service. The purpose of this paper is to show the three options provided by the WCF to treat competition and moreover, showing some of the various synchronization techniques provided by NET Framework and may be used in conjunction with WCF. Importantly, the modes competition discussed here do not guarantee the security of access to some shared resource. It is the responsibility of the developer who is developing the service and, therefore, as discussed above, we make use of existing classes within the NET Framework that allow synchronization and protection of such resources in an environment multi-threading . There are some cases where the combination of management mode instance with the mode of competition already guarantee this and we will see below how to apply them. When a request comes in for a service, one thread is removed from the ThreadPoolto serve the request, more specifically, one thread I / O. When multiple requests arrive at the service and he, in turn, supports the access to multiple requests, then we need to know how to treat them. An important point here is that competition may or may not exist depending on the management mode service instance. To recap, we have three possibilities: PerSession , PerCall and Single . mode PerSession an instance is created and maintained as long as the proxy . If the client uses the same proxy to make multiple calls at the same time, then there may be competition between requests from the same client; already in the model PerCall traditional competition does not exist, because for every call to a particular operation a new service instance is created to meet it. Unfortunately the model PerCall is not entirely sure about the competition problems because it may make the use of a shared resource among multiple calls (such as caching , static variables, etc.). Finally, the model Single mode is the instance management more conducive to problems, since any request will be routed to the same instance and, if shared resources are not properly secured, we have problems of deadlocks or contention. To treat competition WCF provides a property called ConcurrencyMode , which is defined in the attribute ServiceBehaviorAttribute , ie, it is a feature of the service and not the contract. This property accepts one of the three options set the enumeratorConcurrencyMode , which are listed below:
using System;
using System.ServiceModel;
[ServiceBehavior (
InstanceContextMode = InstanceContextMode.PerSession
, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServicoDeClientes: ICliente
{
// Implementation
}
|
Important Note: Before proceeding it is important that you have knowledge in some features offered by .NET Framework for synchronization and resource protection in an environment multi-threading . Some of these features will be discussed in this article, but its operation is not deeply discussed, since it is outside the scope of the article. For a better understanding of these features, you can refer to Chapter 14 of this article . singlecompetition When a service is set to Single , it will be blocked while a request is being met, making it impossible for other requests running at the same time. If they exist, they will be queued waiting for the process to finish they are processed in the order they arrived. This model is the safest, since you will not need to deal with the protection of shared resources, because the WCF itself ensures that only one thread will be processed at a time.Given that the current request is terminated, unlocks the WCF service allowing the next message from the queue (if any) is processed. When this message start to be processed, it blocks the service again, keeping this block until the method is finished. This mode of competition decreases the problems but, likewise, decreases the throughput .Relying on mode Single of competition, when the management model instance of the class is set to PerSessionand any request is made to the service through the same proxy , this second call should wait until the first is completely finished. In the model PerCall there is no competition, there will always be a unique instance for each request and is not shared with any other call. Finally, if the instance management mode is also set to Single , or a single instance serving all requests, only one call is processed at a time, blocking all others, placing them in a row and not having problems . competition with Reentrant Before talking mode Reentrant , you must understand that functionality is provided by WCF and that is the main target of this management mode competition: callbacks .The callbacks enable bidirectional communication between the client and the service and communication are also referred to as duplex . In this type of communication will be necessary to define a callback so that the contract can, at any given time, firing a pre-defined method on the client side. The creation of this type of message is outside the scope of the article and assume that you already have the relevant knowledge. If you do not have, you can refer to this article . This mode of treatment of competition follows the same idea of the previous model (Single ), ie, only one request is processed at a time is allowed; however, this mode has a customization: we briefly leave the service to perform some external task (such as the call to another service) and, via callback , return safely to the same service, continuing the execution of the same, without causing deadlock . Importantly, during this "window", possible requests in the queue awaiting processing may enter the service (in their respective order). When the thread who left the service to perform another task is finished, it will be replaced in the queue to be processed, or better, to continue processing that existed before the callback . When the service is exposed as PerCall and it requires a re -input (this occurs when there callbacks ), then you must set it asReentrant not to cause deadlock with itself; re-entry will not suffer problems with blockages in relation to other requests, because in order PerCall the instance will only serve to that request. But when the management mode instance is defined as PerSession and the competition as Reentrant , the callback will be triggered and when the control back to the service and there is already some other request being executed, the return of the callback will be blocked until the current request is terminated. This feature allows increased throughput to customers who callmulti-threading , while allowing callback is processed, another call to the service can be executed. Finally, the model instance management set to Single , the callback will be fired and, when control returns, he will be queued if there is any request processing. Particular care to be taken when using the mode Reentrant is managing state of internal members used the service. As we saw earlier, once the callback is executed, other calls can occur in parallel, but there is one detail: when the service is running callback , it is important that he keep the service in a consistent state to meet other requests; similarly, when the callback returns, it is necessary to update the information used by the method, because during the external call possibly other calls were made, and may modify the information that he is (was) using. instance of the proxy that started the process should stay active until thecallback is triggered by the service and, if it does not, an exception of type ProtocolException will be triggered.Moreover, it is important to know that the callback is always executed in a thread other than the thread that initiated the call to the method. This means that when we are using this technique in Windows Forms applications, we pay attention to us when we need to access form controls from the method callback . This is due to the fact that the controls are created on a thread different (the same one that initiated the call to the method by proxy ), which requires us to access them from it. There are some features that Windows Forms provides in conjunction with NET Framework, but will not be addressed here. Multiple Use mode competition as Multiple together with the model instance management PerSession or Single allow a single instance meets the diverse requests ( threads ), thus increasing the throughput . The problem in these scenarios is that there is the possibility of the service using a shared resource between these calls, which will force to protect these resources manually, using the existing synchronization techniques. In the model PerCall the option of competition Multiple is irrelevant, since in this way there will always be one serving each request instance. Depending on the resources being used by the service, we should protect them from concurrent access. If you are accessing a resource that already has the proper treatment, then there is nothing to worry about. On the other hand, if the service maintains resources that are accessed by multiple threads , such as internal members of the class, then it is necessary to protect them manually. If you do not do this, it is more likely to occur deadlocks , race conditions and conflicting information during processing. Comparative
| Instance / Competition | Single | Reentrant | Multiple |
| PerSession |
- Limits the throughput in concurrent calls
- Concurrent calls can only be processed between differentproxies
- No need to work with locking mechanisms
|
- Increases thethroughput in concurrent calls
- Allows accessmulti-thread when the callback is running
- You must take special care of the state of internal service members
|
- Allows access multi-threadthrough the same proxy
- No need to work with locking mechanisms
|
| PerCall |
|
- Irrelevant, since there is an instance of exclusive service
|
- Irrelevant, but taking special care when accessing a shared resource and external (static variables)
|
| Single |
- Limits the throughput
- Guarantee that the code will not be accessed by multiple requests at the same time
- No need to work with locking mechanisms
|
- While a callback is running, the next request (if any) will be executed
- Increases thethroughput when there are queued calls
- You must take special care of the state of internal service members
|
- There is no lock to the level of service, multiple requests can access the same instance
- No need to work with locking mechanisms
- The throughput is subject to the blocks used
|
|
Conclusion: This article showed the characteristics of each management mode of competition that exists within the WCF. Although the setting is extremely basic, it dramatically changes the behavior of the service, especially when combined with any of the instance management modes. Moreover, the choice of mode competition will influence how you should write the service implementation, or if disregarding the possible roadblocks that may occur when trying to access shared resource.
Single: The service can accept only one call at a time, not accepting re-entries (used in callbacks ). For messages that arrive while a request is being processed, it waits until the current process is finalized. When the option is not specified, this value is set as the default.
Reentrant: In this model, the service can accept only one call at a time, as well as Single , but allow re-entries are made (used in callbacks ), thus avoiding the deadlock .
Multiple: This model supports multi-threading . This will allow multiple threads to enter the same time in the service and, if any shared resources, need to use techniques to ensure safe access to them, avoiding possible deadlocks .
The example below illustrates how to proceed to configure one of the concurrency management options in the class that represents the service:
Comments
Post a Comment