Enable Transaction in WCF

08 / Feb / 2016 by Mohit Kumar 0 comments

Transaction in WCF is a set of operations or logical unit of work comprising of activities. Here, if a single operation fails, then all operation fails automatically.

We will follow these steps for enable transaction in WCF :

Step 1:- Add  Transactions namespace in  WCF Service project.

Using System.Transaction;

 

Step 2:- Set TransactionFlow property of the OperationContract attribute Mandatory.

options for TransactionFlow are as follows:
a. Mandatory – transaction must be flowed
b. Allowed – transaction may be flowed
c. Not Allowed – transaction is not flowed

Our WCF service contract:

[TransactionFlow(TransactionFlowOptions.Mandatory]
void Showinfo();

 

Step 3:- Now, set the OperationBehavior attribute for the method.

[OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)]
void showinfo()
{
}

 

TransactionScopeRequired = true (it can only be called in a transaction)
TransactionAutoComplete = true (if the operation completes successfully, transaction will be committed.)

 

Step 4:- Enable Transactions for WCF Binding being used.
For Example, In our configuration file (Web.config) bindings will be as follows:

<bindings>

<wsHttpBinding>
<binding name=”httpBinding”  transactionFlow=”true” />
wsHttpBinding >
Note:- That we must choose a binding that supports transactions As Follow

  • netTcpBinding
  • netNamedPipeBinding
  • wsHttpBinding
  • wsDualHttpBinding

 

Step 5:- Need to start the transaction from client side

Import namspance transaction in WCF

using System.Transaction;
Using(TransactionScope  transScope = new TransactionScope())
{
IMyServiceClient client = new IMyServiceClient();
client.showinfo();

transScope.complete();

}

 

 

 

FOUND THIS USEFUL? SHARE IT

Tag -

asp.net WCF

Leave a Reply

Your email address will not be published. Required fields are marked *