A transaction is a collection or group of one or more units of operation executed as a whole. Another way to say it is that transactions provide a way to logically group single pieces of work and execute them as a single unit, or transaction.
To implement transactions in WCF, we utilize TransactionScope class that automatically manages transactions and detect the scope of the transaction.
For example, if you are calling three services methods and third one fails to complete the operation, transaction will be rolled back unless it’s outside the boundary of a TransactionScope.
WCF supports transaction on following bindings:
1. WSHttpBinding
2. NetTcpBinding
3. NetNamedPipeBinding
4. WSDualHttpBinding
5. WSFederationHttpBinding
When you develop a Service Contract, you have to specify the
TransactionFlow attribute on each
Operation Contracts that requires a
Transaction to be handled.
There are following flow options in the TransactionFlow attribute.
- TransactionFlowOption.Allowed: Transaction can be flowed.
- TransactionFlowOption.Mandatory: Transaction must be flowed.
- TransactionFlowOption.NotAllowed: Transaction should not be flowed. This is default.
Now, implement the service and specify
OperationBehavior attribute on each method. You can specify the
TransactionScopeRequired = true / false.
Now enable the Transaction on the binding itself. Open your web.config file and specify the
transactionflow = true
Now add service reference and consume the service. In this way you have implemented trasactions for your wcf services.
How to enable Transactions in WCF?
Follow below steps to enable transactions in WCF.
Step 1: Create Two WCF Services
Step 2: Attribute Interface Methods with TransactionFlow
Step 3: Attribute the Implementation with TransactionScopeRequired
Step 4: Enable Transaction Flow using WCF Service Config File
Step 5: Call the 2 Services in One Transaction
and test if Transaction Works.
We will see all these steps in details in later posts.