WCF Transactions – Easy WCF
Skip to content

WCF Transactions

Definition:

A transaction is a collection or group of operations executed as a whole unit. It provides way to logically group single piece of work and execute them as a single unit. WCF allows client applications to create transactions and to propagate transactions across servers.

Challenges:

Consider a system maintained in consistent state, when application fails to perform particular operation, you should recover and place the system in the consistent state.
While doing singe operation, there will be multiple atomic sub operation. These operations might success or fail. We are not considering about sub operation which are failed. We mainly consider about the success operation. Because we have to recover all these state to its previous consistence state.
Performance will be decreased because you need to execute huge amount of code
So transactions are used to maintain system consistency and handling error-recovery challenges.

Transaction Properties:

Atomic – When transaction completes, all the individual changes made to the resource were all one atomic, indivisible operation.
Consistent – Transactions must leave the system in consistent state.
Isolated – Resources participating in the transaction should be locked and it should not be access by other third party.
Durable – Durable transactions must survive failures.

Enable Transactions for WCF Service:

Now, consider we already have developed a WCF service and we wanted to enable transactions on it. So, we will follow the steps below:

1.Add System.Transactions namespace to WCF Service project.

2.Set TransactionFlow property of the OperationContract attribute to Mandatory.

Available options for TransactionFlow are:
a. Mandatory – transaction must be flowed
b. Allowed – transaction may be flowed
c. Not Allowed – transaction is not flowedFor example, our WCF service contract as follows:

3.Now, set the OperationBehavior attribute for the implementing method.

TransactionScopeRequired = true means it can only be called in a transaction.
TransactionAutoComplete = true means that if the operation completes successfully, transaction will be committed.

4.Enable Transactions for WCF Binding is being used.
For Example, In our configuration file bindings will be as follows.

Remember that we must choose a binding that supports transactions i.e. netTcpBinding, netNamedPipeBinding, wsHttpBinding, wsDualHttpBinding, and wsFederationHttpBinding.

5.Need to start the transaction from client as:

Optionally, If we wanted to specify the Isolation level for the transaction, we can add serviceBehavior attribute to implementing class as follows:

© 2015, admin. All rights reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *