知道代码块是否在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/

10-13 08:24