Chapter 8. Concurrency Management


Incoming client calls are dispatched to the service on threads from the thread pool. Since multiple clients can make multiple concurrent calls, the service itself can sustain those calls on multiple threads. If those calls are dispatched to the same instance, you must provide thread-safe access to the service's in-memory state or risk state corruption and errors. The same is true for the client's in-memory state during callbacks, since callbacks too are dispatched on threads from the thread pool. In addition to synchronizing access to the instance state when applicable, all services need to synchronize access to resources shared between instances, such as static variables or user-interface controls. Another dimension altogether for concurrency management is ensuring that, if required, the service (or the resources it accesses) executes on particular threads.

WCF offers two modes for synchronization. Automatic synchronization instructs WCF to synchronize access to the service instance. Automatic synchronization is easy and simple to use, but it is only available for service and callback classes. Manual synchronization puts the full burden of synchronization on the developer, and requires application-specific integration. The developer needs to employ the .NET synchronization locks, and it is by far an expert discipline. The advantages of manual synchronization are that it is available for service and nonservice classes alike and it allows developers to optimize throughput and scalability. This chapter starts by describing the basic concurrency modes available and then presents more advanced aspects such as resourcing safety and synchronization, thread affinity and custom synchronization context, callbacks, and asynchronous calls. Throughout, the chapter shares best practices and concurrency management design guidelines.




Programming WCF Services
Programming WCF Services
ISBN: 0596526997
EAN: 2147483647
Year: 2004
Pages: 148
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net