Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » WCF » Transaction in Windows Communication Foundation

Transaction in Windows Communication Foundation

In this article I am going to explain step by steps approach to create transaction enabled WCF service.

Page Views : 5461
Downloads : 153
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
OrderManagement.zip
 
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction: 

For building robust and high quality service oriented applications transactions plays important role. Windows Communication Foundation provides simple, declarative transaction support for the developers, enabling you to configure parameters such as transaction flow, transaction scope, Isolation level and transaction time out. In this article I am going to explain step by steps approach to create transaction enabled WCF service.

Summary:

In brief following are the steps to be followed:
  • In service contract at operation contract level
    • Set TransactionFlow property of OperationContract attribute: This adds transaction support to the service contract and enables client to start transaction.
  • In the service at operation behavior level
    • Set TransactionScopeRequired property of OperationBehavior attribute: This adds transaction support to the code that implements interface i.e. service. This enables WCF runtime to include service method in a transaction.
    • Set TransactionAutoComplete property of OperationBehavior attribute: Setting this property to true commits the transaction if no exception occurs.
  • In the service at service behavior level
    • TransactionIsolationLevel property of ServiceBehavior attribute: This specifies transactions locking behavior.
    • TransactionTimeout property of ServiceBehavior attribute: By setting this property to certain time and transaction doesn't complete in specified time it will roll back the transaction.
  • In the configuration file at binding level
    • TransactionFlow property of binding to true: This enables transaction on binding. Following binding supports transaction
      1. NetTcpBinding
      2. NetNamedPipeBinding
      3. WSHttpBinding
      4. WSDualHttpBinding
      5. WSFederationHttpBinding
  • Choose transaction protocol: WCF runtime choose protocol by default. Following are the two protocols :
    • Ole Transactions: This is used when clients are .Net clients.
    • Web Service Atomic clients: This is used when clients are not .Net clients.
Database details:

In this demo, I am going to add data first to Order table and then to OrderDetails table through WCF service. For this demo purpose adding data to Order and OrderDetails table will be two separate calls to WCF service which will be part of transaction.

1.gif
 
Step 1: Set TransactionFlowOption Property 

In the following figure, there is IOrderService contract having two operation contract:
  1. AddOrder: To insert data to Order table
  2. AddOrderDetails : To insert data to OrderDetails table
To both AddOrder and AddOrderDetails method set it's TransactionFlowOption to Mandatory

2.gif

When the operation is configured to:
  • TransactionFlowOption.NotAllowed, the client cannot propagate its transaction to the service. Even if transaction flow is enabled at the binding and the client has a transaction, it will be silently ignored and not propagate to the service. As a result, the service will never use the client's transaction, and the service and the client can select any binding with any configuration.
  • TransactionFlowOption.Allowed, if the client has a transaction, then the service will allow the client's transaction to flow across the service boundary. However, the service may or may not use the client's transaction even though it was propagated. The service can be configured to use any binding, transaction-aware or not, but the client and the service must be compatible in their binding configuration.
  • TransactionFlowOption.Mandatory means the client must have a transaction to propagate to the service. Trying to call a service without a transaction throws an exception on the client. With mandatory flow, the client's transaction always propagates to the service. Once again, the service may or may not use the client's transaction.
Step 2: Set TransactionScopeRequired, TransactionIsolationLevel and TransactionTimeOut Property 

In the OrderService, which s implements IOrderService contract, set:
  • At operation level set TransactionScopeRequired = true and TransactionAutoComplete = true
  • At class level set TransactionIsolationLevel  = Serializable and TransactionTimeOut = 30 seconds
3.gif

Step 3: Set TransactionFlow = true for binding

In the configuration file of the service, set TransactionFlow = true for binding

4.gif 

5.gif
 
Step 4: On client side, add TransactionScope

On client side, call AddOrder and AddOrderDetails method within TransactionScope. This ensures that when executing AddOrder and AddOrderDetails method no exception occurs it's going to commit the transaction.

6.gif
 
Conclusion: In this article, we have seen the step by step process of using transaction in Windows Communication Foundation. In my next article I will try explain how we can use Transaction and Session in WCF. In the attachment, I have included a sample code, tables SQL script.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sanket Terdal
Sanket has over 10 years of IT industry experience. His core technical skills are ASP.Net ,AJAX(Microsoft AJAX Library), Windows Workflow Foundation ,C# and SQL Server 2005. He has earlier worked with major Client’s like IBM , Microsoft, United HealthCare , Sprint and successfully executed projects with latest .Net technologies. His experience involves wide range of domains like insurance, telecom, automobile sectors. Some of his successful projects also involved conversion from Visual Studio 2003 to Visual Studio 2005 and SQL server 2000 to SQL Server 2005. He always keeps working towards performance tuning of various .Net application which ultimately leads to very rich user experience He is also writing technical articles on aspalliance.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.