了解代码块是否在 TransactionScope 内的最佳方法是什么?
Transaction.Current 是一种可靠的方法还是有任何微妙之处?
是否可以通过反射访问内部 ContextData.CurrentData.CurrentScope(在 System.Transactions 中)?如果是,如何?
最佳答案
Transaction.Current
应该是可靠的;我刚刚检查过,这也适用于抑制的交易:
Console.WriteLine(Transaction.Current != null); // false
using (TransactionScope tran = new TransactionScope())
{
Console.WriteLine(Transaction.Current != null); // true
using (TransactionScope tran2 = new TransactionScope(
TransactionScopeOption.Suppress))
{
Console.WriteLine(Transaction.Current != null); // false
}
Console.WriteLine(Transaction.Current != null); // true
}
Console.WriteLine(Transaction.Current != null); // false
关于c# - 如何知道代码是否在 TransactionScope 内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/980337/