I have a WPF application, and in a button click event, I call the WCF service to process the transaction. After completing one transaction, I throw an exception. To see if the transaction will be rolled back or not. But even I received an error, the transaction was still completed, it was not interrupted in the 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(); } } }
App.config already says "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. A transaction does not roll back, even when I raise an exception, the first transaction does not roll back.
source share