I have a WPF application and In button click event I am calling WCF service for handling transaction in it. After one transaction complete I am throwing exception. To see whether transaction will rollback or not. But even I got error still transaction got completed it is not aborted in database.
UI Button event:
private void button1_Click(object sender, RoutedEventArgs e)
{
binding = new WSHttpBinding();
endpoint = new EndpointAddress("http://localhost:6144/EmployeeService.svc/EmployeeService");
ChannelFactory<IEmployeeService> cf = new ChannelFactory<IEmployeeService>(binding, endpoint);
IEmployeeService obj = cf.CreateChannel();
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
obj.UpdateProductData(201, 10);
throw new Exception("Wrong");
obj.UpdateProductData(202, 15);
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}
}
In app.config i already given "transactionFlow="true"
.
WCF Service:
[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
public bool UpdateProductData(int ProdId, int Amount)
{
DALClass objDALProd = new DALClass();
return objDALProd.UpdateProductData(ProdId, Amount);
}
DALClass:
public bool UpdateProductData(int prodID,int Amount)
{
try
{
objComd.Connection = objConn;
objConn.Open();
objComd.CommandText = "UpdateEmployeeData";
objComd.CommandType = CommandType.StoredProcedure;
objParam = new SqlParameter("@ProductId", prodID);
objComd.Parameters.Add(objParam);
objParam = new SqlParameter("@Amount", Amount);
objComd.Parameters.Add(objParam);
objComd.ExecuteNonQuery();
objConn.Close();
}
catch(Exception ex)
{
throw new FaultException("Database Error");
}
return true;
}
Please let me know where my mistake is. Transaction is not rolled back, even when I am raising exception first transaction is not rolled back.