Simple Steps to Enable Transactions in WCF
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:
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:
- Add
System.Transactionsnamespace to WCF Service project. - Set
TransactionFlowproperty of the OperationContract attribute toMandatory.Available options for TransactionFlow are:- Mandatory - transaction must be flowed
- Allowed - transaction may be flowed
- Not Allowed - transaction is not flowed
[TransactionFlow(TransactionFlowOptions.Mandatory] void MyMethod(); - Now, set the
OperationBehaviorattribute 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. - 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. - 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(); }
serviceBehavior attribute to implementing class as follows:[ServiceBehavior(TransactionIsolationLevel=System.Transaction.IsolationLevel.Serializable)]
Public class MyService : IMyService{}
No comments:
Post a Comment