服務與交易
Windows Communication Foundation (WCF) 應用程式可以初始化用戶端內的交易,並協調服務作業中的交易。用戶端可以初始化交易並叫用數個服務作業,同時確保服務作業已認可,或是復原為單一單位。
對於需要用戶端交易的服務作業,您可以指定 ServiceBehaviorAttribute 並設定服務合約的 TransactionIsolationLevel 和 TransactionScopeRequired 屬性,藉此啟用服務合約中的交易行為。TransactionAutoComplete 參數會指定是否要在未擲回任何未處理的例外狀況時,自動完成執行方法的所屬交易。如需 這些屬性的詳細資訊,請參閱 ServiceModel 交易屬性。
在服務作業中執行並由資源管理員所管理的工作 (例如記錄資料庫更新),屬於用戶端交易的一部分。
下列範例示範如何使用 ServiceBehaviorAttribute 和 OperationBehaviorAttribute 屬性來控制服務端的交易行為。
[ServiceBehavior(TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable)]
public class CalculatorService: ICalculatorLog
{
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = true)]
public double Add(double n1, double n2)
{
recordToLog(String.Format("Added {0} to {1}", n1, n2));
return n1 + n2;
}
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = true)]
public double Subtract(double n1, double n2)
{
recordToLog(String.Format("Subtracted {0} from {1}", n1, n2));
return n1 - n2;
}
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = true)]
public double Multiply(double n1, double n2)
{
recordToLog(String.Format("Multiplied {0} by {1}", n1, n2));
return n1 * n2;
}
[OperationBehavior(TransactionScopeRequired = true,
TransactionAutoComplete = true)]
public double Divide(double n1, double n2)
{
recordToLog(String.Format("Divided {0} by {1}", n1, n2));
return n1 / n2;
}
}
您可以設定用戶端與服務繫結使用 WS-AtomicTransaction 通訊協定,並將 <transactionFlow> 項目設定為 true 來啟用交易與交易流程,如下列範例組態所示。
<client>
<endpoint address="net.tcp://localhost/ServiceModelSamples/service"
binding="netTcpBinding"
bindingConfiguration="netTcpBindingWSAT"
contract="Microsoft.ServiceModel.Samples.ICalculatorLog" />
</client>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingWSAT"
transactionFlow="true"
transactionProtocol="WSAtomicTransactionOctober2004" />
</netTcpBinding>
</bindings>
用戶端可以藉由建立 TransactionScope 並叫用交易範圍內的服務作業來開始交易。
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
//Do work here
ts.Complete();
}