::::: Practical Guide to WCF RESTful Service :::::
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.Transactions namespace to WCF Service project.
- Set TransactionFlow property of the OperationContract attribute to Mandatory.
Available options for TransactionFlow are:
a. Mandatory – transaction must be flowed
b. Allowed – transaction may be flowed
c. Not Allowed – transaction is not flowedFor example, our WCF service contract as follows:[TransactionFlow(TransactionFlowOptions.Mandatory]
void MyMethod();
- Now, set the OperationBehavior attribute 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 >
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();}
Optionally, If we wanted to specify the Isolation level for the transaction, we can add serviceBehavior attribute to implementing class as follows:
[ServiceBehavior(TransactionIsolationLevel=System.Transaction.IsolationLevel.Serializable)]
Public class MyService : IMyService{}
This is all we need to do for enabling transactions in WCF.
There are few more things regarding “Service Instancing and Sessions” that need to be considered while working with Transactions but this basic WCF tutorial is more focused on enabling transactions in simple scenario. In my later WCF article on Windows Communication Foundation Transactions on this blog, I’ll discuss those concepts in more details.
Other Related Articles:
- 3 techniqtues for WCF Instance Management
- WCF Hosting (Console | Windows Service | IIS)
- 5 simple steps to create your first WCF RESTful Service
- What is a Known Type?
- What’s New in WCF 4.5?
- 7 jQuery code snippets every web developer must have
- 3 simple steps to create your first ASP.NET Web API service
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
Hello,
Nice.it is helpful for beginners .
Thanks for providing such a good article…