Over a million developers have joined DZone.
Gold Partner

Transactions in WCF

The Mobile Zone is brought to you in partnership with Apptimize.  Discover the 5 little know reasons your app is underperforming and how to better understand your customers mobile needs.

Transaction plays an important role in any business application that involved CRUD operations. 
To implement transactions, we utilize TransactionScope class that automatically manages transactions and detect the scope of the transaction. You can apply TransactionScope on a block of code and regardless of how many WCF services instances you have opened or
operation contracts you are calling will be carried on the scope of that 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

While developing a Service Contract you have to specify the TransactionFlow attribute on each
Operation Contracts that requires a Transaction to be handled. In the below code snippet there are two methods one which submit employee master information and the other that submits employee details. As both require transaction so I have specified TransactionFlow attribute on both of them.

        void CreateEmployee(Common.DataContracts.Employee employee);

        void SubmitEmployeeDetails(EmployeeDetails employeeDetails);

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.

Next, implement the service and specify OperationBehavior  attribute on each method. You can specify the TransactionScopeRequired property as true or false.

[OperationBehavior(TransactionScopeRequired= true)]
public void CreateEmployee(Common.DataContracts.Employee employee){


Now enable the Transaction on the binding itself. Open your web.config file and specify the transactionflow = true as follows

          <binding name="TransactionalBind" transactionFlow="true"/>      

Now add service reference and consume the service. While calling CreateEmployee and SubmitEmployeeDetails method you have to put them in the TransactionScope block and if any of the method fails, the transaction will be rollback.

using (TransactionScope scope = new TransactionScope())

  clientobj = new Services.EmployeeService.EmployeeServiceClient();

catch (Exception ex)


Happy Coding!


The Mobile Zone is brought to you in partnership with Apptimize.  Learn more about app optimization methods that help teams identify user preferences, decide the next iterative changes to make, and validate them through testing.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}