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 ...

Get Programming WCF Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.