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 it is thread-safe without any effort.Keep operations on single-mode sessionful and singleton services short in order to avoid blocking other clients for long.
When using
ConcurrencyMode.Multiple
, you must use transaction auto-completion.Consider using
ConcurrencyMode.Multiple
on per-call services to allow concurrent calls.Transactional singleton service with
ConcurrencyMode.Multiple
must haveReleaseServiceInstanceOnTransactionComplete
set tofalse
:[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.
Multiple
,ReleaseServiceInstanceOnTransactionComplete
=false)]
class MySingleton : IMyContract {...}Never self-host on a UI thread and call the service by the UI application.
Never allow callbacks to the UI application that called the service unless the callback posts the call using
SynchronizationContext.Post( )
.Use the asynchronous calls pattern on the client side only.
When supplying the proxy with both synchronous and asynchronous methods, apply the
FaultContractAttribute
only to synchronous methods.Do not mix transactions with asynchronous calls.
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.