Gaurav Arora is Microsoft Certified Technical Specialist. He is M.Phill(Comp. Sc.), Masters in Computer Applications. Gaurav is an author of various online/offline articles, tutorials, including Silverlight, Asp.net, WCF, LINQ etc. He is the Founder, Webmaster and Sr. Editor of Technical communities viz. Silverlight Club and Dotnetspider .
Gaurav is the co-founder of MsDotNetHeaven a free I.T. online educational web portal.
Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
In simple words we can define a transaction as :Transaction is a group of various operations, executed as a bunch or whole.
In detailed way you can think an online shopping cart where one purchased some accessories and made the payment and both the vendor and customer get debited viz. product and amount, now suppose during this exercise if connection fails then what happened, no one happy ?
Introduction Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
The above task can be made happy to everyone if this contain some undo / rollback things, which means if due to some avoidable/un-avoidable reasons connection breaks / fails then all entries would be marked as reversed. This bunch of collective operation is nothing but the transaction.
Now, we can define Transaction as :
"Transaction is a collection or group of one or more units of operation executed as a whole. Another way to say it is that transactions provide a way to logically group single pieces of work and execute them as a single unit, or transaction."
What is Transaction? Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
Transactions are the key to building robust, high-quality service-oriented applications. Windows® Communication Foundation provides simple, declarative transaction support for service developers, enabling you to configure parameters such as transaction flow and voting, all outside the scope of your service. In addition, Windows Communication Foundation allows client applications to create transactions and to propagate them across service boundaries.
A new namespace System.Transactions was released in .Net2.0, which makes transaction programming easy and efficient.
Windows Communication Foundation utilizes the many available objects of this namespace to provide all the necessary transaction capabilities you will need when building your WCF services and client applications
StreamReader sw = new StreamReader(@"E:shuby utorialarticlenames.txt");
In this example, the TransactionAutoCompleteOnSessionClose property is set to true, the TransactionIsolationLevel is set to ReadCommitted, and the TransactionTimeout is set to 30 seconds. The TransactionTimeout property value is of a Timespan object.
The [TransactionFlow] attribute is used to specify the level at which a service operation can accept a transaction header. This attribute has a single property and is the attribute used to annotate a service operation method.
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.
Let’s consider 3 computers as shown in the previous slide. 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 already discussed 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.
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-II : 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.
public interface IService1
Step-III : 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.
Step-V : 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.
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.