
dotnet,mobile,wcf,transactions,.net c#,transactionscope

Transactions in WCF

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!


{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}


{{ parent.views }} ViewsClicks