今天整理的內容是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.
不要把事務和異步調用混用