1.分布式Transaction地实现需要Two-phase commit protocol 和 负责管理Transaction的第三方的Transaction Manager。
2Transaction 的Reliable和TransactionFlow设置
<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端新建一个Transactionclass MyService : IMyContract { [OperationBehavior(TransactionScopeRequired = true)] public void MyMethod( ) { Transaction transaction = Transaction.Current; Debug.Assert(transaction != null); } }
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( );