Handling Transaction in WCF
How would you feel, when you ordered something, you gotta money-debited text peeping out from your mobile but couldn’t see a 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?
It might be because of whole lot of reason, 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 hatred on making a online transaction further. As a 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, lets 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 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 there involved a loss of material & time. Hence we come to know that transaction plays a vital role in Online commerce.
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 of operation should be executed successfully else if one failed the whole set of operation 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 in respective to success and failure of transaction.
Isolated : Isolating the transaction i.e making it confidential till we have a successful transaction. So that partial comittments are kept private from clients.
Durable : The transaction should sustain even in 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, 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 been handled by the Microsoft Framework, in-order to get essence out of it, we should handle the Transaction explicitly in WCF.
In-order to enable the transaction in WCF, there 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 manipulate the data and propagates the result to the Service. So a developer should understand about the Transaction Propagation involved.
Also the default binding in WCF i.e. BasicHttpBinding wont explicitly supports Transaction, so to get the things work we need to make use of the below bindings available in WCF.
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 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 transaction in WCF, please check out another WCF Tutorial “5 Simple Steps to Enable Transactions in WCF”.
Once you are comfortable with Enabling Transaction 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.
Voting Phase :
As we saw earlier that, transaction basically involves Server side and Client side where the service and consumption happens. Here the protocol accommodate those with Co-ordinator (Server) a master system and Cohorts (Clients) which are in one or more numbers. The voting system works in way to ensure there is no failure. Co-ordinator shoots out a message to all the Cohorts and asks for Commit. Co-ordinator set 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.
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 act 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.
- If you arestatingOperationBehaviour 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 there happens no exceptions.
- TransactionScopeRequired : This property specifies the requirement of transaction for the decorated method.
- 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 be happen seamlessly.
- TransactionFlowOption.Mandatory – As the name suggest, 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 the to propagate its own transactions.
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 overheaded, it saves money & time.
- Provides resource sharing among many users.
Hope this WCF tutorial gives you a detail touch up about WCF transaction, protocol and propagation. Kindly let us know if you have any questions.
Happy WCF learning!