Click here to Skip to main content
15,891,951 members
Articles
(untagged)

Handling Transaction in WCF

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Jul 2015CPOL6 min read 8.3K   3  
Handling transaction in WCF

How would you feel when you ordered something, you got money-debited text peeping out from your mobile but couldn’t see an order successful page while shopping at your favorite portal? Don’t you yell at the portal which didn’t send you the product and the bank which took your bucks for no reason! So what causes this frustration?

WCF - Handling Transactions

It might be because of a whole lot of reasons, it might be because of sudden power outage, hardware crash, network jam or it could be because of natural disaster as well. The reasons can be many but if the transaction was handled in the way it should be, the guy who ordered wouldn’t be frustrated and develop hatred for making an online transaction further. As computer professionals, we should definitely know about Online transactions and its management, more specifically, WCF transaction and its Propagation & Protocols.

What is Meant by Transaction?

Transaction can be explained as a set of one or more atomic units operations that are executed all as a whole. To be more practical, let's take the example stated above and consider that you are anxiously awaiting for your order-successful page but you only get the debited message from bank. It means that either of the operations failed, here the failure is on e-commerce portal side. The two actions here are considered as one and it should be executed as a whole, if either one failed, the transaction should be aborted, otherwise a loss of material & time was involved. Hence, we come to know that transaction plays a vital role in Online commerce.

Transactions in WCF

ACID Characteristics of Transaction

Transaction can be classified clearly with its ACID properties. The abbreviation states four different properties namely:

  • Atomicity: This property explains the same as stated above, i.e., Either the operation should be executed successfully, else if one failed, the whole set of operations should be aborted and changes made till now should be reverted back.
  • Consistency: The transaction should deliver a consistent outcome as expected in both cases. For instance, if the ordered product’s cost is $50 and your bank balance is $750. The outcome expected should be either $700 or $750 respectively for success and failure of the transaction.
  • Isolated: Isolating the transaction, i.e., making it confidential till we have a successful transaction so that partial commitments are kept private from clients.
  • Durable: The transaction should sustain even in the midst of external failures. Hence, it can be relied on further. Handling in all gateways makes a transaction durable.

Transactions in WCF

A WCF service is where a Service is created and it will be consumed by one or more Clients. The transaction happens at two points here, i.e., Service level and Client level. The basic transaction properties are being handled by the Microsoft Framework, in-order to get an essence out of it, we should handle the Transaction explicitly in WCF.

In-order to enable the transaction in WCF involves a Configuration with the application.

Here, the Service can manipulate the logic and sends to the Client or else it can be like Client validates and manipulates the data and propagates the result to the Service. So, a developer should understand the Transaction Propagation involved.

Also the default binding in WCF, i.e., BasicHttpBinding won't explicitly support Transaction, so to get the things working, we need to make use of the below bindings available in WCF.

  • NetTcpBinding
  • NetNamedPipeBinding
  • WSHttpBinding
  • WSDualHttpBinding
  • WSFederationHttpBinding

Note: By default, the transaction is disabled. You can follow here to understand about choosing the most appropriate binding for a WCF Service in various scenarios.

Glimpse On Enabling Transaction

Transaction in WCF was introduced in version 2.0. It can be included into the project with namespace System.Transactions. It was supported by many platforms such as SQLServer, MSMQ, ADO.NET, etc. The three prominent attributes involved in enabling Transactions are:

  • ServiceBehaviour Attribute
  • OperationBehaviour Attribute
  • TransactionFlow Attribute

To know more about enabling transactions in WCF, please check out another WCF Tutorial “5 Simple Steps to Enable Transactions in WCF”.

Once you are comfortable with Enabling Transactions in WCF, let us dive into 2PC protocol where the Transaction process is based on.

Two Phase Committed Protocol (2PC)

The Two Phase Committed protocol, also abbreviated as 2PC is a set of constraints involved in controlling and handling Transaction process. This protocol tries to avoid failure and recover from it. 2PC can be broadly classified into two main phases such as Voting phase and Completion phase.

Two Phase Committed Protocol in WCF

Voting Phase

As we saw earlier that transaction basically involves Server side and Client side where the service and consumption happens. Here, the protocol accommodates those with Co-ordinator (Server) a master system and Cohorts (Clients) which are in one or more numbers. The voting system works in a way to ensure there is no failure. Co-ordinator shoots out a message to all the Cohorts and asks for Commit. Co-ordinator sets to commit only if the Cohorts votes as Yes, it doesn’t set to commit if it votes No or Failure to reply.

Completion Phase

The Co-ordinator sends Commit message to all the Cohorts connected. The Co-ordinator commits only if it gets the acknowledgment from the Cohorts. If it receives No or times-out, then it sends out a Rollback message to all the Cohorts where the Cohorts revert with the help of Log file which it maintains from during the Initial phase.

Transaction Propagation

WCF propagation or act of spreading the transaction across the service boundary enables both Client and Service to participate in each other’s action, i.e., Triggers a service to participate in a client’s transaction and in the same way, it paves a path to include operations on multiple services in the same transaction.

Configuration for Enabling Transaction Propagation

The WCF checks for this configuration while processing transaction and acts accordingly based on the setup. Let us discuss about the basic configuration involved.

  • First and foremost step involved in enabling the Transaction propagation is to trigger on the TransactionFlow in the bindings for relevant transaction support protocol. Open Web.Config file and add the following XML snippet, which states transactionFlow to True.

    Enable Binding for Transaction Flow

  • If you are stating OperationBehaviour in your project, WCF provides us two transaction related properties such as:
    • TransactionAutoComplete: If this property is used, the transaction will be auto-completed if no exceptions happen.
    • TransactionScopeRequired: This property specifies the requirement of transaction for the decorated method.
      WCF OperationBehavior Attribute
  • Once the bindings and behaviors are handled, the next important step is to decorate Service Operations with TransactionFlow Attribute. They are three in types such as:
    • TransactionFlowOption.Allowed – This attribute allows the client transaction to happen seamlessly.
    • TransactionFlowOption.Mandatory – As the name suggests, the client and Service must use the transaction based on the binding it is configured.
    • TransactionFlowOption.NotAllowed – This attribute if used will not allow the client to propagate its own transactions.

      TransactionFlow in WCF

Benefits of WCF Transaction Propagation

  • Job processing and handling are processed when the resources are not so busy.
  • Idle time of Computer resources are minimized.
  • Since the resources are less overhead, it saves money & time.
  • Provides resource sharing among many users.

Hope this WCF tutorial gives you a detailed touch up about WCF transaction, protocol and propagation. Kindly let us know if you have any questions.

Happy WCF learning!

The post Handling Transaction in WCF appeared first on WCF Tutorial.

License

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


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
-- There are no messages in this forum --