Search code examples
c#transactionstransactionscope

TransactionScope guard check


I'm working on a legacy project with almost non existent transactions, which leads to a lot of partial commits in the event of an error. The SQL calls are a mix of raw ADO, Linq to Entities and EntityFramework, and I'd like to get these running in transactions.

To tackle this problem, I'm looking at going down the TransactionScope route. Can anyone tell me if it's possible to do some kind of guard check to determine in a method if it is being run from inside a TransactionScope? Either using an attribute or some kind of check on the TransactionScope object itself. The end goal being to throw some kind of meaningful exception and track down areas that are missing transactions.

Thanks


Solution

  • If your code is running inside a TransactionScope block, then Transaction.Current != null. You'll need to add a using for System.Transactions and a reference to System.Transactions.dll .