Click here to Skip to main content
Click here to Skip to main content

6 Steps to Enable Transactions in WCF

By , 26 Nov 2013
Rate this:
Please Sign up or sign in to vote.

Table of Contents

Introduction and Goal

In this article, we will try to understand how we can implement transactions in WCF service. So we will create two WCF services which do database transactions and then unite them in one transaction. We will first understand the 6 important steps to enable transactions in WCF services. At the end of the article, we will try to force an error and see how the transaction is rolled back after the error.

Now a days, I am distributing my 400 questions and answers ebook which covers major .NET related topics like WCF, WPF, WWF, Ajax, Core .NET, SQL Server, Architecture and a lot more. I am sure you will enjoy this ebook. You can download my Ebook from here.

Step 1: Create Two WCF Services

The first step is to create two WCF service projects which will participate in one transaction. In both of these WCF services, we will do database transactions and we will try to understand how a WCF transaction unifies them. We have also created a web application with name WCFTransactions which will consume both the services in one transaction scope.

Step 2: Attribute Interface Methods with TransactionFlow

In both the WCF services, we will create a method called UpdateData which will insert into the database. So the first thing is to create the interface class with ServiceContract attribute and the method UpdateData with OperationContract attribute. In order to enable transaction in UpdateData method, we need to attribute it with TransactionFlow and we have specified that transactions are allowed for this method using TransactionFlowOption.Allowed enum.

[ServiceContract]
public interface IService1
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void UpdateData();
}

Step 3: Attribute the Implementation with TransactionScopeRequired

The 3rd step is to attribute the implementation of the WCF services with TransactionScopeRequired as true. Below is the code snippet which has a simple database inserting function, i.e. UpdateData which is attributed by TransactionScopeRequired attribute.

[OperationBehavior(TransactionScopeRequired = true)]
public void UpdateData()
{
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("insert into Customer
	(CustomerName,CustomerCode) values('sss','sss')",objConnection);
objCommand.ExecuteNonQuery();
objConnection.Close();
}

Step 4: Enable Transaction Flow using WCF Service Config File

We also need to enable transactions for wsHttpBinding by setting the transactionFlow attribute to true.

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

The transaction enabled binding we need to attach with the end point through which our WCF service is exposed.

<endpoint address="" binding="wsHttpBinding" 
	bindingConfiguration="TransactionalBind" contract="WcfService1.IService1">

Step 5: Call the 2 Services in One Transaction

Now that we are done with enabling our server side transaction, it’s time to call the above 2 services in 1 transaction. We need to use the TransactionScope object to group the above 2 WCF services in one transaction. To commit all the WCF transactions, we call the Complete method of the Transactionscope object. To rollback, we need to call the Dispose method.

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{

// Call your webservice transactions here
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}

Below is the complete code snippet in which we have grouped both the WCF transactions in one scope as shown below:

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
obj.UpdateData();
ServiceReference2.Service1Client obj1 = new ServiceReference2.Service1Client();
obj1.UpdateData();
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}

Step 6: Test If Your Transaction Works

It’s time to test if the transactions really work. We are calling two services, both of which are doing an insert. After the first WCF service call, we are forcing an exception. In other words, the data insert of the first WCF service should revert back. If you check the database records, you will see no records are inserted by the WCF service.

Click to enlarge

History

  • 6th August, 2009: Initial post
  • Can I update this article? It's not mine!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
GeneralMy vote of 3 PinprofessionalBala pendrive26-Nov-13 22:05 
QuestionTransaction flow between 2 WCF services PinmemberJason Henderson21-Oct-13 5:09 
AnswerRe: Transaction flow between 2 WCF services PinmemberMember 436622026-Nov-13 7:48 
GeneralRe: Transaction flow between 2 WCF services PinmemberJason Henderson26-Nov-13 9:14 
GeneralMy vote of 4 PinmemberSenthilkumar Rajagopal10-Oct-13 6:56 
GeneralMy vote of 5 PinmemberManuele Camilletti5-Jun-13 0:35 
GeneralMy vote of 5 PinmemberRahul.KumarSharma18-Dec-12 19:09 
It's good.
GeneralMy vote of 4 Pinmembersameer@accenture12-Jun-12 0:21 
GeneralMy vote of 5 PinmemberSiddheshwar Duchal from Pune14-May-12 21:11 
GeneralMy vote of 5 PinmemberMember 408801229-Sep-11 19:58 
Questionts.Dispose(); PinmemberMaxim Novak6-Mar-11 1:27 
GeneralMy vote of 4 PinmemberAfter205014-Dec-10 22:23 
GeneralCan't rollback transaction Pinmembertiennl_hsd15-Sep-09 23:19 
GeneralRe: Can't rollback transaction PinmemberShivprasad koirala15-Sep-09 23:22 
GeneralRe: Can't rollback transaction Pinmembermmccgg17-Sep-09 23:37 
GeneralRe: Can't rollback transaction PinmemberAbhijeet Yadav2-Nov-09 19:20 
GeneralRe: Can't rollback transaction Pinmemberkmanikandan.2010@gmail.com28-Dec-09 21:14 
GeneralRe: Can't rollback transaction Pinmemberkmanikandan.2010@gmail.com28-Dec-09 22:12 
GeneralRe: Can't rollback transaction PinmemberKent K7-Jun-11 15:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 26 Nov 2013
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid