如何知道代码是否在 TransactionScope 内?

2024-01-09

了解代码块是否在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何知道代码是否在 TransactionScope 内? 的相关文章

随机推荐