Commit a TransactionScope only after successful work

Disposing a TransactionScope does not automatically approve its work. Call Complete() only after every required operation succeeds. Leaving the scope without that vote causes the transaction to abort.

When the scope contains await, explicitly enable asynchronous transaction flow. Resources must support ambient enlistment to participate.

Observe the lifecycle

Run this complete example in a .NET console project:

C#
using System.Transactions;

using (var scope = new TransactionScope(
	TransactionScopeOption.Required,
	new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
	TransactionScopeAsyncFlowOption.Enabled)) {
	Console.WriteLine(Transaction.Current is not null);
	await Task.Delay(10);
	Console.WriteLine(Transaction.Current is not null);
	scope.Complete();
}
Console.WriteLine(Transaction.Current is null);

The output is three True lines. This demonstrates ambient context and disposal. It deliberately performs no database write.

Apply it to database work

Open an enlistment-capable connection inside the scope, execute the required commands, then call Complete() as the final successful statement. Let failures escape the scope before handling them. Do not swallow an error and vote to commit incomplete work. A surrounding transaction can still determine the final outcome.

Several connections or resource managers may promote a transaction to a distributed one, subject to provider and platform support. For work on one connection, an explicit database transaction often makes ownership clearer. Filesystem writes and ordinary HTTP requests do not become transactional merely because this scope surrounds them.

Official references

Implementing an Implicit Transaction using Transaction Scope - .NET Framework
Implement an implicit transaction using the TransactionScope class in .NET. This class provides a way to mark a block of code as participating in a transaction.
https://learn.microsoft.com/en-us/dotnet/framework/data/transactions/implementing-an-implicit-transaction-using-transaction-scope
TransactionScope lifecycle
TransactionScopeAsyncFlowOption Enum (System.Transactions)
Specifies whether transaction flow across thread continuations is enabled for TransactionScope.
https://learn.microsoft.com/en-us/dotnet/api/system.transactions.transactionscopeasyncflowoption
TransactionScopeAsyncFlowOption

Author: Herman Schoenfeld