下面一节是队列服务与安全。
本系列相关文章:
Transactions
事务
1. Never manage transactions directly.
不要直接管理事务
2. Apply the
TransactionFlow
attribute on the contract,not the service class.
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.
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.
17. Strive to use the
TransactionalBehavior
attribute to manage transactions on sessionful services:
[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.
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.
5. Consider using
ConcurrencyMode.Multiple
on per-call services to allow concurrent calls.
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.
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.
10. Keep asynchronous operations short. Do not equate asynchronous calls with lengthy operations.
11. Do not mix transactions with asynchronous calls.
不要把事务和异步调用混用
原文链接:https://www.f2er.com/javaschema/287584.html