Transaction

Transaction is the option in which we execute multiple queries as a single unit.

It means either all queries will be completed successfully or none of query will complete if any single query having any error.

Suppose I have two following Tables:-

create table User_detail(login_id int primary key, name varchar(100))

create table login_detail(login_id int foreign key references User_detail(login_id), user_pwd varchar(100))

		

In these tables we want that when user submit the form then data will be inserted in both table, it means user personal detail will be stored in user_detail table and password detail will be stored in login_detail table.

So we want both operation will be performed simultaneously, if any operation get error then both queries will be dispose.

We use Transaction to perform this task in database.

As we know WCF is Distributed architecture so to perform transaction in WCF we have to perform following steps:-

Transaction implementation in WCF

First of all open WCF application by clicking on visual studio->file->new->Website->Wcf Service

Add insert method in IService Interface

[ServiceContract]
public interface IService
{

	[OperationContract]
   
  	int InsertData(string query);

}
		

The method on which you want to activate transaction you have to add TransactonFlow attribute.

[ServiceContract]
public interface IService
{

	[OperationContract]
   
    [TransactionFlow(TransactionFlowOption.Mandatory)]
	int InsertData(string query);

}

		

There are three options available in TransactionFlowOption

		wcf Transaction step1
		
		Figure 1
		

In Allowed option there is a chances that whether transaction will work or not. In NotAllowed option it means transaction will not work and in Mandatory option transaction will work.

Now set the TransactionScopeRequired as True in OperationBehaviour attribute in the following manner:-

public class Service : IService
{
    [OperationBehavior(TransactionScopeRequired=true)]
    public int InsertData(string query)
    {
        SqlConnection con = new SqlConnection("Data Source=isha;Initial Catalog=wcfData;User ID=isha123;Password=techaltum");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = query;
        cmd.Connection = con;
       int id= cmd.ExecuteNonQuery();
       return id;

    }
}

	

Note: - This is the example to understand the Transaction in WCF not to understand how we insert data. You can add your own security feature while working with database.

Now enable the Transaction in WCF Binding by customizing the binding which we you want to use for communication. Note:-Make sure that this binding support Transaction or not.

		<bindings>
      <wsHttpBinding>
        <binding name="trans" transactionFlow="true"/>
      </wsHttpBinding>
    </bindings>
    <services>
		

. Note: - Add this node in System.ServiceModel tag.

Now add endpoint in System.ServiceModel

<endpoint address="" binding="wsHttpBinding" contract="IService" bindingConfiguration="trans">
	

Now execute this code

wcf Transaction step2
		
		Figure 2
		

Now create client and add service Reference

To enable transaction in WCF we need TransactionScope class and this class is available in System.Transactions namespace.

To add this namespace we add the transaction dll by adding the reference of System.Transactions

Right click on your project->add Reference

		wcf Transaction step3
		
		Figure 3
		

Now add System.Transactions namespace

		wcf Transaction step4
		
		Figure 4
		

Now click ok and create the object of TransactionScope and call the service

		protected void Page_Load(object sender, EventArgs e)
    {

        using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
        {
            try
            {

                ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
                int r = svc.InsertData("insert into User_detail(login_id, name) values(101,'isha')");
                r = svc.InsertData("insert into login_detail(login_id,user_pwd values(101,'isha123')");
                ts.Complete();
                Response.Write("Data Inserted!!!!");
            }
            catch (Exception)
            {
                Response.Write("Data Not Inserted!!!!");
                ts.Dispose();
            }

        
        
        }
    }

		

If you will execute the both query then data will get inserted.

		wcf step 5
		
		Figure 5
		

Now check in database

		wcf step 6
		
		Figure 6
		

Now make some mistakes in queries

		wcf step 7
		
		Figure 7
		

I make mistake in second query. First query run successfully but it will simply dispose the first query.

Now again execute the code and change some data.

		wcf step 8
		
		Figure 8
		

In this way we can implement the transaction in wcf.

Email Address

For any query you can mail me at Malhotra.isha3388@gmail.com.