Mobile Zone is brought to you in partnership with:

Experienced Solution Architect and Programmer, Microsoft MVP, Speaker and Author I have more than 10 years of software development experience working on Microsoft technologies. I have extensive experience working with custom developed Windows and Web based .Net projects with primarily C# as a language. My major interests involve in designing frameworks and architecture for software projects. I am a learning passionate and always wants to explore new and upcoming Microsoft technologies. I also run a Meetup group named as “Microsoft Technology Practices User Group”. The vision of this group is to share and explore latest technologies on Microsoft platform. We used to provide trainings, demo sessions, technology overview in group meetings and we also welcome professionals to present and give an insight on any technology in which they are expert and present. Moreover, our group has been sponsored by Plural Sight which helps us to share and learn from the webcasts published there. If anyone is interested to join my Meetup group please visit the following site http://meetup.com/MSTPUG or send me an email at ovaismehboob@yahoo.com to become a group member. Ovais is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Transactions in WCF

12.26.2012
| 6169 views |
  • submit to reddit

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.

        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        void CreateEmployee(Common.DataContracts.Employee employee);

        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        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

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

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())
{
try
{

 Services.EmployeeService.EmployeeServiceClient()
  clientobj = new Services.EmployeeService.EmployeeServiceClient();

 clientobj.CreateEmployee(employeeObj);
 clientobj.CreateEmployeeDetails(employeeObjDetails);
 scope.Complete();
}
catch (Exception ex)
{
  scope.Dispose();

}
}

Happy Coding!



 







Published at DZone with permission of Ovais Mehboob Ahmed Khan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)