Thursday, November 18, 2010

How to implement transaction in WCF client

Here we see how to consume two wcf service from wcf client !

Create a WCF Service :

using System.ServiceModel;
namespace ACWCF
{ [ServiceContract]
public interface ITranService
{

[TransactionFlow(TransactionFlowOption.Allowed)]
[OperationContract]
void UpdateData();
}
}

// Implement the interface
public class TranService : ITranService
{
[OperationBehavior(TransactionScopeRequired = true)]
public void UpdateData()
{
SqlConnection objConnection = new SqlConnection("MyConnectionString");
objConnection.Open();
SqlCommand objCommand = new SqlCommand("insert into Customer(CustomerName,CustomerCode) values('sss','sss')", objConnection);
objCommand.ExecuteNonQuery();
objConnection.Close();
}

// Assuming we are aware of hosting the service on IIS .. so i am not talking about the web.config part of it .



Now lets create a client to consume multiple wcf service

// this is how wcf client can consume multiple wcf service.
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
{
try
{
//ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
//obj.UpdateData();
//ServiceReference2.Service1Client obj1 = new ServiceReference2.Service1Client();
//obj1.UpdateData();

ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}

A great article can be seen on
http://www.c-sharpcorner.com/UploadFile/shivprasadk/436456754708092009145905PM/4364567547.aspx

another good wcf clip can be seen at:

No comments:

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC