今天整理的内容是WCF事务和并发管理相关的设计规范。WCF服务编程设计规范(5):事务与并发管理设计。中英文对照,How to design Transactions and Concurrency Management in WCF Service.
下面一节是队列服务与安全。
本系列相关文章:
Transactions
事务
1. Never manage transactions directly.
不要直接管理事务
2. Apply the
TransactionFlow
attribute on the contract, not the service class.
在契约而不是服务类上标注
TransactionFlow
属性,
3. Do not perform transactional work in the service constructor.
不要在服务的构造函数里执行事务操作
4. Using this book’s terminology, configure services for either Client or Client/Service transactions. Avoid None or Service transactions.
使用本书中的术语,为客户端或客户端
/
服务端事务模式配置服务。避免使用
None
或服务事务模式。
5. Using this book’s terminology, configure callbacks for either Service or Service/Callback transactions. Avoid None or Callback transactions.
使用本书中的术语,为服务或服务
/
回调事务模式配置回调。避免使用
None
或回调事务模式。
6. When using the Client/Service or Service/Callback mode, constrain the binding to flow transactions using the
BindingRequirement
attribute.
当使用客户端
/
服务或服务
/
回调模式,通过
BindingRequirement
属性约束绑定传播事务
7. On the client, always catch all exceptions thrown by a service configured for None or Service transactions.
在客户端,始终捕获
None
或服务事务里抛出的所有异常。
8. Enable reliability and ordered delivery even when using transactions.
即使使用事务的时候,也要启用可靠性和顺序传递
9. In a service operation, never catch an exception and manually abort the transaction:
在服务操作里,不要捕获异常并手动终止事务
//Avoid:
避免
[OperationBehavior(TransactionScopeRequired = true)]
public void MyMethod()
{
try
{
...
}
catch
{
Transaction.Current.
Rollback
();
}
}
10. If you catch an exception in a transactional operation, always rethrow it or another exception.
如果你在一个事务性操作里捕获了异常,始终直接重新抛出这个异常或者抛出另外一个异常
11. Keep transactions short.
保持事务简洁
12. Always use the default isolation level of
IsolationLevel.Serializable
.
使用
IsolationLevel.Serializable
默认的事物隔离级别
13. Do not call one-way operations from within a transaction.
不要在事务内部调用一个单向操作
14. Do not call nontransactional services from within a transaction.
不要在一个事务内部调用一个非事务服务
15. Do not access nontransactional resources (such as the filesystem) from within a transaction.
不要在一个事务内访问非事务性资源(比如文件系统)
16. With a sessionful service, avoid equating the session boundary with the transaction boundary by relying on auto-complete on session close.
对于会话服务,避免因为会话关闭时的
auto-complete
就把会话边界和事务边界等价。
17. Strive to use the
TransactionalBehavior
attribute to manage transactions on sessionful services:
尽量使用
TransactionalBehavior
属性去管理会话服务上的事务
[Serializable]
[TransactionalBehavior]
class MyService : IMyContract
{
public void MyMethod()
{...}
}
18. When using a sessionful or transactional singleton, use volatile resource managers to manage state and avoid explicitly state-aware programming or relying on WCF’s
instance deactivation on completion.
当使用会话或事务性的单例服务时,使用易失资源管理器去管理状态,并且避免显示地使用状态编程或者在完整时依赖
WCF
实例钝化机制,
19. With transactional durable services, always propagate the transaction to the store by setting
SaveStateInOperationTransaction
to
true
.
在持久性事务服务里,始终通过设置
SaveStateInOperationTransaction
为
true
来把事务和传播到存储系统中。
Concurrency Management
并发管理
1. Always provide thread-safe access to:
对以下资源始终提供线程安全的访问
a) Service in-memory state with sessionful or singleton services
会话或者单例服务在内存中的状态
b) Client in-memory state during callbacks
回调期间客户端在内存中的状态
c) Shared resources
共享资源
d) Static variables
静态变量
2. Prefer
ConcurrencyMode.Single
(the default). It enables transactional access and provides thread safety without any effort.
默认情况推荐使用
ConcurrencyMode.Single
模式。它会启用事务性访问并提供线程安全,而不会带来任何开销。
3. Keep operations on single-mode sessionful and singleton services short in order to avoid blocking other clients for long.
当操作在是单个会话模式或者单例模式时,请保证操作简短,以长时间免阻塞客户端。
4. When using
ConcurrencyMode.Multiple
, you must use transaction autocompletion.
当使用
ConcurrencyMode.Multiple
时,你必须启用事务
autocompletion
自动提交机制。
5. Consider using
ConcurrencyMode.Multiple
on per-call services to allow concurrent calls.
在单调服务上使用
ConcurrencyMode.Multiple
属性,允许并发调用,
6. Transactional singleton service with
ConcurrencyMode.Multiple
must have
ReleaseServiceInstanceOnTransactionComplete
set to
false
:
使用
ConcurrencyMode.Multiple
的
事务性单例服务必须把
ReleaseServiceInstanceOnTransactionComplete
设置为
false
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.
Multiple
,
ReleaseServiceInstanceOnTransactionComplete
=
false)]
class MySingleton : IMyContract
{...}
7. Never self-host on a UI thread, and have the UI application call the service.
不要在
UI
线程上托管服务,让
UI
程序只调用服务。
8. Never allow callbacks to the UI application that called the service unless the callback posts the call using
SynchronizationContext.Post()
.
从不允许服务回调
UI
程序,除非回调使用了
SynchronizationContext.Post()
.
9. When supplying the proxy with both synchronous and asynchronous methods, apply the
FaultContract
attribute only to synchronous methods.
当代理提供了异步和同步方法的时候,在同步方法上使用
FaultContract
10. Keep asynchronous operations short. Do not equate asynchronous calls with lengthy operations.
保持异步调用操作简短。不要把异步调用和冗长的操作混淆。
11. Do not mix transactions with asynchronous calls.
不要把事务和异步调用混用
本文转自 frankxulei 51CTO博客,原文链接:
http://blog.51cto.com/frankxulei/318793
,如需转载请自行联系原作者