Programming WCF Services - Transactions

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( );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值