Tuesday, December 25, 2012

Transaction in WCF: Code Sample examples, Use and Implementation

Use of Transactions in WCF
Before discussion over transaction in WCF, Let us know some basics about both of these topics.

WCF
WCF is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.

Transaction
I am working on a project where I need to execute two or more SQL commands in such a way that if any one of the statements fails, then no other statement will be able to change the database.  So this is the core requirement of transaction to handle this kind of situations. 

WCF provides a single unified system for all activities whether it is a database related or not. To use transaction in services the service should support to transactions. WCF transactions uses WS specifications. 

To allow transaction in wcf we have to use:
1) Transaction Flow attribute along with contract in our interface
2) You have to set the isolation level property, we have 6 levels of isolation
  • Chaos: - pending changes from more highly isolated transactions cannot be overridden.
  • Read Committed: - Volatile data can be modified but it cannot be read during the transaction.
  • Read Uncommitted: - Volatile data can be read as well as modified during the transaction.
  • Repeatable Read: - Volatile data can be read but not modified during the transaction and new data can be added.
  • Serializable: - Volatile data can be only read. However, no modifications and adding of new data is allowed.
  • Snapshot: - Volatile data can be read. However, before modifying the data it verifies if any other transaction had changed data. If yes then it raises error.
By default, the System. Transactions infrastructure creates Serializable transactions. 

Steps: To Create Transactions 
public interface IService{
   [OperationContract]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    void UpdateMyData();
 

Implement the same method in class with TransactionScopeRequired attribute as 
[OperationBehavior (TransactionScopeRequired= true)]
    
    public void UpdateMyData()
    { 
        // your logic
    }
 
Then make sure transaction flow is enable in web.config file as 
<wsHttpBinding>
    <binding name="MyTransactions" transactionFlow="true"/>
  wsHttpBinding>

How to Call in Client Program You can commit all wcf transaction with the help of complete method of TransactionScope Object. 
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try{

//webservice transactions calls here.. 
//in below line ts is the object of TransactionScope ts.Complete();
}
catch (Exception ex)
{
     ts.Dispose();
}
}
------------------------------------------------------------------------------------------------

0 comments:

 
Powered by Code Imagine