WCF Transactions

WCF Transactions


Transactions consists of set of activities in which all activities succeeds or fails.
Transactions follows ACID properties .

  1. Atomicity ( A ) - means all transactions are either committed or rolled back to previous state

  2. Consistency ( C ) - means changes made by transactions causes transformation from one state to other

  3. Isolation ( I ) - prevents one transaction to see uncommitted changes belonging to other concurrent transaction

  4. Durability ( D ) - means updates are persistent even in case of failure

To add transaction support to a WCF service, you will take the following actions:


  1. Add transaction support to the service contract. This is required.

  2. Add transaction support to the code that implements the service contract. This is required.

  3. Configure transactions in the implementation code. This is optional.

  4. Enable transactions on the binding. This is required.


Step 1

Create a Service Contract with two Operation Contract


We will create a service contract with two operations .

[OperationContract]
Employee SaveEmployeeData(Employee empData);
[OperationContract]
void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary );
 
SaveEmployeeData to save Employee Details like ID , Name and Role.
SaveEmployeeSalary to save Employee Details like ID and Salary.
 
Step 2

Add Transaction Support to the Operation Contract


To add transaction support to operation contract , we have to use TransactionFlow attribute .
The TransactionFlow attribute specifies whether the operation supports transactions. There are three possible values for this attribute:
  1. NotAllowed : The operation cannot participate in a transaction. This is the default value for this attribute.
  2. Allowed : The operation will participate in a transaction if the client creates one.
  3. Mandatory : In order to call this operation, the client must create a transaction.  
When the operation has TransactionFlowOption.NotAllowed attribute , the client cannot propagate its transaction to the service. Even if transaction flow is enabled at the binding and the client has a transaction, transaction will not propagate to the service. TransactionFlowOption.NotAllowed is the default TransactionFlowOption value of the TransactionFlow attribute.


TransactionFlowOption.Allowed means the client may or may not have a transaction to propagate to the service. Trying to call a service without a transaction does not throw an exception on the client. With Allowed flow, the client’s transaction always propagates to the service. Once again, the service may or may not use the client’s transaction.

TransactionFlowOption.Mandatory means the client must have a transaction to propagate to the service. Trying to call a service without a transaction throws an exception on the client. With mandatory flow, the client’s transaction always propagates to the service. Once again, the service may or may not use the client’s transaction.
 
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
Employee SaveEmployeeData(Employee empData);


[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary );

Step 3

Add TransactionScopeRequired Attribute to the Implementation


TransactionScopeRequired Attribute can be set to true or false. To enable transaction set  TransactionScopeRequired attribute to true.

[OperationBehavior(TransactionScopeRequired = true)]
public Employee SaveEmployeeData(Employee empData)
{
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("INSERT INTO Employee values ('" + empData.EmpID + "','" + empData.EmpName + "','" + empData.EmpRole + "')", objConnection);
objCommand.ExecuteNonQuery();
objConnection.Close();
return empData;

}
[OperationBehavior(TransactionScopeRequired = true)]
public void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary )
{
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("INSERT INTO EmployeeSalary values ('" + empData.EmpID + "','" + empSalary.EmpSalary + "','" + empSalary.EmpCurrency + "')", objConnection);
objCommand.ExecuteNonQuery();
objConnection.Close();

}

Step 4

Enable Transaction Flow using WCF Service Configuration File



We also need to enable transactions for wsHttpBinding by setting the transactionFlow attribute to true.

<bindings>
<wsHttpBinding>
<binding name="TransactionalBind" transactionFlow="true"/>
</wsHttpBinding>
</bindings>

The transaction enabled binding we need to attach with the end point through which our WCF service is exposed.

<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="TransactionalBind" contract="WcfService1.IService1">

Step 5


Call the two Operations in One Transaction


Below is Client code to call these operations :
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
obj.SaveEmployeeData(empData);
obj.SaveEmployeeSalary(empData , empSalary);
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}

WCF Tutorial

No comments:

Post a Comment

Labels

.NET Framework Interview Questions (7) .NET Interview Questions (10) .NET Remoting Interview Questions (1) ADO.NET and BLOB Error (1) ADO.NET Interview Questions (4) Agile Articles (9) AJAX Articles (5) AJAX Interview Questions (11) Algorithms (2) Analytics Articles (2) Analytics Interview Questions (3) Android FAQs - Part 1 (2) Articles (13) ASP.NET Articles (24) ASP.NET Error and Resolution (4) ASP.NET Interview Questions (23) ASP.NET Tutorial (8) AWS Interview Questions (16) Business Analyst Interview Questions (1) Cloud Computing Interview Questions (16) CSharp Articles (17) CSharp Interview Questions (32) CSharp Tutorial (17) Data Analysis (2) Data Structure (1) Design Pattern Articles (5) DevOps Tutorial (1) Digital Marketing Interview Questions (1) Download Templates (1) Error Resolution (6) Excel Articles (9) Excel Macros (1) Excel Tips and Tricks (10) HTML5 Interview Questions (3) HTML5 Tutorial (3) Interview Preparation (2) Interview Questions (24) Introduction to Business Analytics (10) Introduction to Python (7) Introduction to R Programming (23) JAVA Articles (6) Java Tutorial (5) LINQ Articles (4) LINQ Interview Questions (2) LINQ Tutorial (3) Microservices Interview Questions (1) MVCInterviewQuestions (2) OOPs Interview Questions (4) Oracle 9i Tutorial (14) Oracle Articles (2) Oracle Interview Questions (15) Outlook Error (1) PHP Interview Questions (3) PHP Tutorial (3) Product Management (12) Product Management Interview Questions (14) Product Owner Interview Questions (2) Program Management (5) Project Management (13) Project Management Articles (34) Project Management Interview Questions (25) Quiz (1) RallyDev Help (1) Scrum Master Interview Questions (11) Selenium Tutorial (1) Sharepoint Articles (1) SQL Interview Questions (23) SQL Server Articles (20) SSIS Interview Questions (6) SSRS Interview Questions (1) Technical Program Management (12) Technical Program Management - Interview Questions (24) TechnicalProgramManagement (5) Threading Interview Questions (2) Tutorial (8) UML Articles (3) UML Interview Questions (2) Unix (3) UNIX Tutorial (3) WCF Articles (20) WCF Interview Questions (9) WCF Quiz (2) WCF Tutorial (16) Web Service Articles (5) Web Service Interview Questions (3) Window Azure (1) XML Articles (6) XML Interview Questions (3) XML Tutorial (3)