Programming WCF Services - Transactions

来源:互联网 发布:知羽电子相册模板 编辑:程序博客网 时间:2024/06/05 15:13

1.分布式Transaction地实现需要Two-phase commit protocol 和 负责管理Transaction的第三方的Transaction Manager。

2Transaction 的ReliableTransactionFlow设置

<netTcpBinding>
   <binding name = "TransactionalTCP"
                                  transactionFlow = "true">
      <reliableSession enabled = "true"/>
   </binding>
</netTcpBinding><netTcpBinding>
   <binding name = "TransactionalTCP"
                                  transactionFlow = "true">
      <reliableSession enabled = "true"/>
   </binding>
</netTcpBinding>

3.允许客户端的Transaction能够进入Service:TransactionFlow(设置在Interface Contract的Function上),不能够和One-Way一起设置([OperationContract(IsOneWay = true)])

[ServiceContract]interface IMyContract{   [OperationContract]   [TransactionFlow(TransactionFlowOption.Allowed)]   void MyMethod(...);}
4.Ambient Transaction的设置TransactionScopeRequired 
如果客户端已经存在Transaction,则传播到Service,WCF设置Client Transaction 作为 Ambient Transaction.
否则,将在Service端新建一个Transaction
class MyService : IMyContract{   [OperationBehavior(TransactionScopeRequired = true)]   public void MyMethod( )   {      Transaction transaction = Transaction.Current;      Debug.Assert(transaction != null);   }}
5 .Transaction modes as product of binding, contract, and behavior

其中Client/Service是 最decoupled 设置

Binding transaction flow

TransactionFlowOption

TransactionScopeRequired

Transaction mode

False

Allowed

False

None

False

Allowed

True

Service

False

NotAllowed

False

None

False

NotAllowed

True

Service

True

Allowed

False

None

True

Allowed

True

Client/Service

True

Mandatory

False

None

True

Mandatory

True

Client


6,对于Transaction,建议以Per-Call方式,简单,并且生命周期能保持一致。
MyContractClient proxy = new MyContractClient( );using(TransactionScope scope = new TransactionScope( )){   proxy.MyMethod(...);   scope.Complete( );}using(TransactionScope scope = new TransactionScope( )){   proxy.MyMethod(...);   scope.Complete( );}proxy.Close( );
原创粉丝点击