Click here to Skip to main content
Click here to Skip to main content
Go to top

WCF FAQ Part 5 – Transactions

, 9 Aug 2009
Rate this:
Please Sign up or sign in to vote.
WCF FAQ Part 5 – Transactions
This is an old version of the currently published article.

WCF FAQ Part 5 – Transactions

Introduction and Goal

WCF is new to me I am not in a position to read this article?

Which protocol is used to handle transactions in WCF?

What are the two different kinds of phases in WCF transactions?

Where does the transaction manager reside?

Can you explain in depth how prepare and commit phases work?

Can we implement transactions in one-way WCF service calls?

Can we see simple WCF example of transactions using SQL Server database?

What the different transaction options ?

Source code 

Introduction and Goal
 

This is my 5th WCF FAQ article. This article is completely dedicated to WCF transactions. In this article we will understand the WCF transaction protocol, two phases in WCF transaction, transaction options and finally end up with simple sample for WCF transaction.

Watch my 500 videos on Azure, WCF, WPF, LINQ, Design patterns, WWF, Silverlight, UML @ http://www.questpond.com 
 

WCF is new to me I am not in a position to read this article?
 

If you are completely new to WCF then this article will not suffice, my suggestion would be to go through some of the below article for basics.

WCF FAQ Part 1 :- This is a 20 question FAQ for beginners which explains basic concepts of WCF like End points, contracts and bindings. It also discusses about various hosting methodologies of WCF service. The article finally ends talking about bindings and one ways operations in WCF. http://www.codeproject.com/KB/aspnet/WCF.aspx 

WCF FAQ Part 2 :- This FAQ covers 10 questions which talks about concepts like duplex contracts , hosting WCF on different protocols , MSMQ bindings , transaction isolation levels and two way communication. The article finally ends talking about two queues volatile and dead letter queue. http://www.codeproject.com/KB/aspnet/WCFPart2.aspx 

WCF FAQ Part 3 ( Security FAQ ) :- In this article we will start with transport and message security understanding. We will then see simple code samples of how to implement transport and message security using WsHTTP bindings. We will also see what is the difference between ‘BasicHttpBinding’ and ‘WsHttpBinding’ with the help of a simple source code. WCF security is a huge topic by itself, but we are sure with this article you will get a quick start of how to go about WCF security. WCFFAQPart3.aspx

WCF Tracing FAQ part 4 :-In this article we will look how we can trace and debug information in WCF services. There are some ready made tracelisteners provided by WCF. The base of these ready made trace listeners is .NET trace listener. So we will first understand the basic concept of trace listener and then go through the ready made tracelisteners provided by WCF. WCFTracingFAQ.aspx 
 

Which protocol is used to handle transactions in WCF?
 

WCF follows WS-* specifications. So it uses WS-Atomic protocol to managed transaction across WCF services. So you can have different WCF services hosted on different computers and they all can run under one transaction unit. The best part of WS-Atomic protocol is that in one transaction you can have heterogeneous WCF services developed in different platform. In other words you can have JAVA and .NET web services running under one transaction. WS-* specifications are globally approved standards to create services.

What are the two different kinds of phases in WCF transactions?
 

WCF transactions follow 2 phase commit. So there are 2 phases one is the prepare phase and the other is the commit phase. All co-ordination of transactions is done by the transaction manager.
 

In prepare phase the transaction manager checks whether all entities are prepared to commit. In commit phase the actual commit starts. You can think about prepare phase as a check saying that all entities are ready to commit and in the commit phase we do the actual work.
 

Where does the transaction manager reside?
 

Transaction manager resides at the client computer who initiates the transaction.
 

Can you explain in depth how prepare and commit phases work?
 

Let’s consider 3 computers as shown in the below figure. The client consuming the WCF service resides in computer ‘A’ while computer ‘B’ and ‘C’ have the WCF services. The transaction is initiated from the computer ‘A’. So as we said previously there are 2 phase one is the prepare phase and the other commit phase. In prepare phase computer ‘A’ sends messages to all the WCF services saying, are they ready to commit?. Once all WCF services respond saying that they are ready for commit it starts the second phase i.e. In the second phase the WCF client issues a commit command. All the WCF services start execution and once they are done they revert back saying they have committed. When all the WCF services revert saying they have committed the transaction is marked as successful.
 

Can we implement transactions in one-way WCF service calls?
 

As we have already seen in the previous questions that WCF transactions are 2-phase commits. In other words for every commit you need to get a confirmation response, saying is the commit successful. In one-way WCF services we do not get any response back , so WCF transactions are not possible with 1 way WCF service calls.
 

Can we see simple WCF example of transactions using SQL Server database?
 

To enable WCF transaction is a 6 step procedure. So let’s create two WCF services and let’s try to call them in one transaction.

Step 1 :- Create two WCF service
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 service in one transaction scope.
 

Step 2:- Attribute interface methods with TransactionFlow
 

In both the WCF service we will create a method called as ‘UpdateData’ which will do insert in to 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 our server side transaction enablement, 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 does your transaction work
 

It’s time to test if the transactions really work. We are calling two services both of these services 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.
 

What the different transaction options ?
 

In the previous code we have use ‘TransactionFlowOption’. We can specify transaction in 3 ways in WCF:-

TransactionFlowOption.NotAllowed

This is a default option. Using this option no transaction will be propagated across the binding. If any client attempts to call the WCF service in a transaction it will be ignored for this option.

TransactionFlowOption.Allowed

This option specifies that client can call this WCF service in a transaction. It’s not compulsory that the service needs to be called in a transaction. You can call without the transaction also.

TransactionFlowOption.Mandatory

This option specifies that client must call the WCF service in a transaction mode. If the WCF service is called without transaction, ‘FaultException’ will be raised.
 

Source code
 

You can find the source code for this article from here

 

License

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

Share

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


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionROFL ONLY AND IDIOT WOULD MAKE WCF SERVICE A WEBPROJECT ROFL PinmemberMember 1046575613-Dec-13 19:27 
GeneralMy vote of 5 Pinmember$andesh M Patil27-Nov-13 3:24 
GeneralRe: My vote of 5 PinmemberMember 1046575613-Dec-13 19:28 
QuestionTransactionScope class not found [modified] Pinmemberbubun sinha19-Sep-13 20:20 
AnswerRe: TransactionScope class not found Pinmember$andesh M Patil27-Nov-13 3:22 
GeneralMy vote of 5 PinmemberAbhishek Ranjan Srivastava11-Sep-13 19:37 
BugUsing and IDisposable Pinmemberatlaste24-Oct-12 20:57 
QuestionGood Article PinmemberAlireza_136222-May-12 2:52 

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
Web04 | 2.8.140916.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid