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

标签 c# .net transactionscope

了解代码块是否在 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/

相关文章:

c# - 实现 IEnlistmentNotification 时我应该在哪里执行操作?

c# - NAudio频带强度

c# - DbConnection.Open 是否总是打开一个新的数据库连接,或者它是否可以重用连接池之一?

c# - 基于类的GUI代码生成

c# - Linq 查询最近 7 天的不同日期值,运行查询时抛出异常

nunit - 这安全吗? - NUnit 基类打开并回滚 TransactionScope

c# - 将 TransactionScope TransactionInformation 事务标识符与 sql server 事务 ID 链接起来

c# - 您如何随机将整数中的一位归零?

c# - 处理来自另一个函数的异常

JavaScript + SQL、.NET