Concurrency Management
Always provide thread-safe access to:
Service in-memory state with sessionful or singleton services
Client in-memory state during callbacks
Shared resources
Static variables
Prefer
ConcurrencyMode.Single(the default). It enables transactional access and provides thread safety without any effort.Keep operations on single-mode sessionful and singleton services short in order to avoid blocking other clients for long.
When you are using
ConcurrencyMode.Multiple, you must use transaction auto-completion.Consider using
ConcurrencyMode.Multipleon per-call services to allow concurrent calls.Transactional singleton service with
ConcurrencyMode.Multiplemust haveReleaseServiceInstanceOnTransactionCompleteset tofalse:[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple,ReleaseServiceInstanceOnTransactionComplete=false)]class MySingleton : IMyContract {...}Never self-host on a UI thread, and have the UI application call the service.
Never allow callbacks to the UI application that called the service unless the callback posts the call using
SynchronizationContext.Post().When supplying the proxy with both synchronous and asynchronous methods, apply the
FaultContractattribute only to synchronous methods.Keep asynchronous operations short. Do not equate asynchronous calls with lengthy operations.
Do not mix transactions with asynchronous calls.