如何知道代码是否在 TransactionScope 内?
了解代码块是否在 TransactionScope 内的最佳方法是什么?
Transaction.Current 是一种可靠的方法吗?或者有什么微妙之处?
是否可以通过反射访问内部 ContextData.CurrentData.CurrentScope (在 System.Transactions 中)? 如果是,怎么办?
What is the best way to know if the code block is inside TransactionScope?
Is Transaction.Current a realiable way to do it or there are any subtleties?
Is it possible to access internal ContextData.CurrentData.CurrentScope (in System.Transactions) with reflection? If yes, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Transaction.Current
应该是可靠的; 我刚刚检查过,这对于抑制交易也适用:Transaction.Current
should be reliable; I've just checked, at this works fine with suppressed transactions, too:这是更可靠的方法(正如我所说,Transaction.Current 可以手动设置,这并不总是意味着我们真的在 TransactionScope 中)。 也可以通过反射获取此信息,但发出 IL 的速度比反射快 100 倍。
Here is more reliable way (as I said, Transaction.Current can be set manually and it doesn't always mean we are really in TransactionScope). It's also possible to get this information with reflection, but emiting IL works 100 times faster than reflection.
有使用表达式的更新版本,不需要
System.Transactions
引用。There is updated version using Expressions which do not need
System.Transactions
reference.