我有一个 WPF 应用程序,并且在按钮单击事件中,我正在调用 WCF 服务来处理其中的事务。一笔交易完成后,我抛出异常。查看事务是否会回滚。但即使我收到错误,事务仍然完成,它不会在数据库中中止。
UI 按钮事件:
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 我已经给出了"transactionFlow="true"
。
WCF 服务:
[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
public bool UpdateProductData(int ProdId, int Amount)
{
DALClass objDALProd = new DALClass();
return objDALProd.UpdateProductData(ProdId, Amount);
}
DAL类:
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;
}
请让我知道我的错误在哪里。事务不会回滚,即使我提出异常第一个事务也不会回滚。