我在我们代码库中寻找一个可能存在的问题,开发人员忘记用大括号括起来 using
语句的内容:
using (TransactionScope transactionScope = new TransactionScope(Transaction.Current))
try
{
RecordUserConnectionCore(context, userName);
transactionScope.Complete();
}
catch
{
transactionScope.Dispose();
throw;
}
try/catch代码块是否在using语句内执行?事务范围是否被正确处理?
请注意,这个问题与try/catch块是否在using上下文中执行有关。我之所以问,是因为try/catch代码没有大括号包围。
Dispose()
调用是多余的。这意味着整个 try/catch 块完全没有意义,因为它除了不必要的 Dispose() 调用之外,只是重新抛出异常。 - itsme86