Chapter 8. Concurrency Management

Incoming client calls are dispatched to the service on threads from the Windows I/O completion thread pool (the pool has 1,000 threads by default). Multiple clients can make multiple concurrent calls, and the service itself can sustain those calls on multiple threads. If the calls are dispatched to the same service 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 I/O completion thread pool. In addition to synchronizing access to the instance state when applicable, all services also need to synchronize access to resources shared between instances, such as static variables. 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 simple to use, but it is available only for service and callback classes. Manual synchronization, on the other hand, puts the full burden of synchronization on the developer and requires application-specific integration. The developer needs to employ .NET synchronization locks, which is by far an expert discipline. The advantages of manual synchronization are that it is available ...

Get Programming WCF Services, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.