Introduction:
In this article,i am going to explain about how to perform transactions in wcf using asp.net and c#.
Main:
Transactions are fundamental to applications in order to ensure consistent behavior for data.
In addition, they are a fundamental building block for ensuring the implementation of atomic,
consistent, independent, and durable (ACID) behavior in an application.
WCF supports WebServices-Atomic transaction protocol.System.ServiceModel namespace helps us to
perform transaction in wcf,and also it contains 3 main components for supporting transactions.
There are,
ServiceBehavior attribute,
OperationBehavior attribute,
TransactionFlow attribute
The available transactionflow options are allowed,notallowed,mandatory.For performing transactions
in wcf service please set transactionflow.mandatory in operation contract,
See this below demo,
Step 1: Create a service with transaction flow,

copy and paste the below code,
using System;
using System.ServiceModel;
using System.Transactions;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Globalization;
namespace TransactionDemoSvc
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
[ServiceContract]
public interface IEmpService
{
[OperationContract]
[TransactionFlow (TransactionFlowOption.Mandatory)]
void InsertEmployee(string Query);
[OperationContract]
[TransactionFlow (TransactionFlowOption.Mandatory)]
void UpdateEmployee(string Query);
}
[ServiceBehavior(
TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable ,
TransactionTimeout = "00:00:30",
ReleaseServiceInstanceOnTransactionComplete = false,
TransactionAutoCompleteOnSessionClose = false,
IncludeExceptionDetailInFaults = true)]
public class EmpService : IEmpService
{
[OperationBehavior(TransactionScopeRequired=true)]
public void InsertEmployee(string Query)
{
SqlConnection local_conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True");
local_conn.Open();
SqlCommand local_cmd = new SqlCommand(Query, local_conn);
local_cmd.ExecuteNonQuery();
local_conn.Close();
}
[OperationBehavior(TransactionScopeRequired=true)]
public void UpdateEmployee(string Query)
{
SqlConnection local_conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True");
local_conn.Open();
SqlCommand local_cmd = new SqlCommand(Query, local_conn);
local_cmd.ExecuteNonQuery();
local_conn.Close();
}
}
}
using System; using System.ServiceModel; using System.Transactions; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Globalization; namespace TransactionDemoSvc { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. [ServiceContract] public interface IEmpService { [OperationContract] [TransactionFlow (TransactionFlowOption.Mandatory)] void InsertEmployee(string Query); [OperationContract] [TransactionFlow (TransactionFlowOption.Mandatory)] void UpdateEmployee(string Query); } [ServiceBehavior( TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable , TransactionTimeout = "00:00:30", ReleaseServiceInstanceOnTransactionComplete = false, TransactionAutoCompleteOnSessionClose = false, IncludeExceptionDetailInFaults = true)] public class EmpService : IEmpService { [OperationBehavior(TransactionScopeRequired=true)] public void InsertEmployee(string Query) { SqlConnection local_conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True"); local_conn.Open(); SqlCommand local_cmd = new SqlCommand(Query, local_conn); local_cmd.ExecuteNonQuery(); local_conn.Close(); } [OperationBehavior(TransactionScopeRequired=true)] public void UpdateEmployee(string Query) { SqlConnection local_conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True"); local_conn.Open(); SqlCommand local_cmd = new SqlCommand(Query, local_conn); local_cmd.ExecuteNonQuery(); local_conn.Close(); } } } |
Step 2:Host the Service,
File –> New –> Project and select console application and named it wcfhost,
steps involved,
1.Add System.ServiceReference,
2.Add TransactionDemoSvc dll,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Transactions;
using TransactionDemoSvc;
namespace WcfHost
{
class Program
{
static void Main(string[] args)
{
ServiceHost local_sh;
Uri local_Namedpipe = new Uri("net.pipe://localhost/NetNamedPipeBinding");
Uri local_tcp = new Uri("net.tcp://localhost:8000/TcpBinding");
local_sh = new ServiceHost(typeof(EmpService), local_tcp, local_Namedpipe);
//NetNamedPipeBinding local_pb = new NetNamedPipeBinding();
NetTcpBinding local_tcpb = new NetTcpBinding();
ServiceMetadataBehavior local_mbehave = new ServiceMetadataBehavior();
local_sh.Description.Behaviors.Add(local_mbehave);
local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
//local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");
//local_sh.AddServiceEndpoint(typeof(IEmpService), local_pb, local_Namedpipe);
local_sh.AddServiceEndpoint(typeof(IEmpService), local_tcpb, local_tcp);
//TransactionFlowAttribute objattr = new TransactionFlowAttribute(TransactionFlowOption.Mandatory);
local_tcpb.TransactionFlow = true;
local_sh.Open();
Console.WriteLine("Service Running");
Console.ReadLine();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Transactions; using TransactionDemoSvc; namespace WcfHost { class Program { static void Main(string[] args) { ServiceHost local_sh; Uri local_Namedpipe = new Uri("net.pipe://localhost/NetNamedPipeBinding"); Uri local_tcp = new Uri("net.tcp://localhost:8000/TcpBinding"); local_sh = new ServiceHost(typeof(EmpService), local_tcp, local_Namedpipe); //NetNamedPipeBinding local_pb = new NetNamedPipeBinding(); NetTcpBinding local_tcpb = new NetTcpBinding(); ServiceMetadataBehavior local_mbehave = new ServiceMetadataBehavior(); local_sh.Description.Behaviors.Add(local_mbehave); local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); //local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex"); //local_sh.AddServiceEndpoint(typeof(IEmpService), local_pb, local_Namedpipe); local_sh.AddServiceEndpoint(typeof(IEmpService), local_tcpb, local_tcp); //TransactionFlowAttribute objattr = new TransactionFlowAttribute(TransactionFlowOption.Mandatory); local_tcpb.TransactionFlow = true; local_sh.Open(); Console.WriteLine("Service Running"); Console.ReadLine(); } } } |

Step 3:Create a client,

Steps Involved,
1.Make wcfhost service is running,
2.Select add reference and add the empservice,

client app code,
ServiceReference1.EmpServiceClient objsvcclient = new ServiceReference1.EmpServiceClient();
using (TransactionScope local_trans = new TransactionScope())
{
string Query1 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')",
6454,
"SoftWares",
"700000",
"James");
string Query2 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')",
6455,
"SoftWares",
"600000",
"Nancy");
string Query3 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')",
66455,
"IT",
"5000",
"Peter");
objsvcclient.InsertEmployee(Query1);
objsvcclient.InsertEmployee(Query2);
objsvcclient.InsertEmployee(Query3);
local_trans.Complete();
}
MessageBox.Show ("Transaction Complete");
ServiceReference1.EmpServiceClient objsvcclient = new ServiceReference1.EmpServiceClient(); using (TransactionScope local_trans = new TransactionScope()) { string Query1 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')", 6454, "SoftWares", "700000", "James"); string Query2 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')", 6455, "SoftWares", "600000", "Nancy"); string Query3 = String.Format("Insert into emp(empid,deptname,salary,manager) values ({0},'{1}','{2}','{3}')", 66455, "IT", "5000", "Peter"); objsvcclient.InsertEmployee(Query1); objsvcclient.InsertEmployee(Query2); objsvcclient.InsertEmployee(Query3); local_trans.Complete(); } MessageBox.Show ("Transaction Complete"); |

Amiable fill someone in on and this fill someone in on helped me alot in my college assignement. Say thank you you on your information.