Transaction in WCF is a set of operations or logical unit of work comprising of activities. Here, if a single operation fails, then all operation fails automatically.
We will follow these steps for enable transaction in WCF :
Step 1:- Add Transactions namespace in WCF Service project.
Step 2:- Set TransactionFlow property of the OperationContract attribute Mandatory.
options for TransactionFlow are as follows:
a. Mandatory – transaction must be flowed
b. Allowed – transaction may be flowed
c. Not Allowed – transaction is not flowed
Our WCF service contract:
Step 3:- Now, set the OperationBehavior attribute for the method.
TransactionScopeRequired = true (it can only be called in a transaction)
TransactionAutoComplete = true (if the operation completes successfully, transaction will be committed.)
Step 4:- Enable Transactions for WCF Binding being used.
For Example, In our configuration file (Web.config) bindings will be as follows:
<binding name=”httpBinding” transactionFlow=”true” />
Note:- That we must choose a binding that supports transactions As Follow
Step 5:- Need to start the transaction from client side
Import namspance transaction in WCF
Using(TransactionScope transScope = new TransactionScope())
IMyServiceClient client = new IMyServiceClient();