Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7673 |  Welcome, Guest!   Register  Login
 Home > Blogs > WCF > Simple steps to enable transactions in WCF ...
Imghani

Simple steps to enable transactions in WCF

 Blog author: Imghani | Posted on: 8/5/2012 | Category: WCF Blogs | Views: 1214 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Transaction is basically a logical unit of work comprising of activities that all needed to be succeeded or failed, and also it must be compliant with ACID principals.

Movement of money from a bank account to another is a simple example of a transaction. In this single transaction, two operations will be performed. One account will be debited (amount will be taken from) and other will be credited (amount will be deposited).

Enabling transactions in Windows Communication Foundation is simple and straight forward but implementation sometimes becomes difficult depending upon the scenario. For example, implementing transactions in a distributed environment will definitely require effort and more things to consider.

Now, consider we already have developed a WCF service and we wanted to enable transactions on it. So, we will follow the steps below:

  1. Add System.Transactions namespace to WCF Service project.

  2. Set TransactionFlow property of the OperationContract attribute to Mandatory.
    Available options for TransactionFlow are:
    a. Mandatory - transaction must be flowed
    b. Allowed - transaction may be flowed
    c. Not Allowed - transaction is not flowed

    For example, our WCF service contract as follows:
    [TransactionFlow(TransactionFlowOptions.Mandatory]
    void MyMethod();

  3. Now, set the OperationBehavior attribute for the implementing method.
    [OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)]
    void MyMethod()
    {
    }
    TransactionScopeRequired = true means it can only be called in a transaction.
    TransactionAutoComplete = true means that if the operation completes successfully, transaction will be committed.

  4. Enable Transactions for WCF Binding being used.
    For Example, In our configuration file bindings will be as follows:
    <bindings>
      <wsHttpBinding>
         <binding name="httpBinding"  transactionFlow="true" />
      </ wsHttpBinding >
    </bindings>
    Remember that we must choose a binding that supports transactions i.e. netTcpBinding, netNamedPipeBinding, wsHttpBinding, wsDualHttpBinding, and wsFederationHttpBinding.

  5. Need to start the transaction from client as:
    using System.Transaction;
    Using( var transScope = new TransactionScope())
    {   
    
         //Calling service methods
         IMyServiceClient client = new IMyServiceClient();
         client.MyMethod();
        
         transScope.complete();
       
    } 
Optionally, If we wanted to specify the Isolation level for the transaction, we can add serviceBehavior attribute to implementing class as follows:

[ServiceBehavior(TransactionIsolationLevel=System.Transaction.IsolationLevel.Serializable)]
Public class MyService : IMyService{}

This is all we need to do for enabling transactions in WCF.
There are few more things regarding "Service Instancing and Sessions" that need to be considered while working with Transactions but this WCF tutorial is more focused on enabling transactions in simple scenario. In my later article on Windows Communication Foundation Transactions on this blog, I'll discuss those concepts in more details.




Imran Abdul Ghani
http://www.topwcftutorials.net
Found interesting? Add this to:


About Imran Ghani

Experience:11 year(s)
Home page:http://www.topwcftutorials.net
Member since:Sunday, June 03, 2012
Level:Starter
Status: [Member]
Biography:Working as Sr. Software Engineer in Emaratech, Dubai, UAE. You can reach his blogging at WCF Tutorials.

 Responses

Srilu.Nayini577
Posted by: Srilu.Nayini577 | Posted on: 8/6/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

It's very useful article.

SRILATHA
.Net Developer

Sarah
Posted by: Sarah | Posted on: 9/7/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

Thanks for all the information,it was very helpful and i really like that you are providing information on .net training ,being enrolled in .net freshers training with projects live training http://www.wiziq.com/course/57-fresher-training-projects i was looking for such .net fresher training to assist me and your information helped me a lot.Really like that you are providing such information . Thanks.

sarah

>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 12:33:05 AM