Whilst teaching developmentors enet 3 class this week I had an idea for a new snippet. It came to me whilst teaching the deck on System.Transactions, I was in the middle of demonstrating to them how you need to be really careful not to cause a transaction to be promoted to the DTC ( See Previous Blog Article ). Two strategies I suggest are
- Stop the DTC, net stop MSDTC
- Add an Debug.Assert statement inside your TransactionScope block to ensure the current transaction is not a distributed transaction, by checking that the DistributedIdentifier is an empty GUID.
The first option is reasonable if you never need the DTC, but if you do have bits of code that does need the DTC its clearly not an option. The second approach is a bit tedious ut if you use a snippet, then its not all that bad. Below is the snippet code..
<Description>Creates a transaction scope and ensures at the end of the block that the transaction is only a local transaction </Description>
using (TransactionScope $scope$ = new TransactionScope())
Debug.Assert(Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty,
"Unexpected!! Transaction is now distributed");