Callbacks and Client Safety
There are quite a few cases when a client might receive
concurrent callbacks. For instance, if the client has provided a
callback reference to multiple services, those services could call back
to the client concurrently. Even if it has only provided a single
callback reference, the service might launch multiple threads and use
all of them to call on that single reference. Duplex callbacks enter the
client on worker threads, and if they are processed concurrently without
synchronization they might corrupt the client’s state. The client must
therefore synchronize access to its own in-memory state, as well as to
any resources the callback thread might access. Similar to a service, a
callback client can use either manual or declarative synchronization.
The CallbackBehavior
attribute
introduced in Chapter 6
offers the ConcurrencyMode
and the
Use
Synchronization
Context
properties:
[AttributeUsage(AttributeTargets.Class)] public sealed class CallbackBehaviorAttribute : Attribute,... { public ConcurrencyMode ConcurrencyMode {get;set;} public bool UseSynchronizationContext {get;set;} }
Both of these properties default to the same values as with the
ServiceBehavior
attribute and behave
in a similar manner. For example, the default of the ConcurrencyMode
property is ConcurrencyMode.Single
, so these two
definitions are equivalent:
class MyClient : IMyContractCallback
{...}
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Single
)] class MyClient : IMyContractCallback ...
Get Programming WCF Services, 3rd Edition 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.