WCF服务编程设计规范(5):事务与并发管理设计

简介:



今天整理的内容是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 ,如需转载请自行联系原作者

相关文章
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
102 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
849 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1046 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
993 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
978 0