-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Labels
Pri3Indicates issues/PRs that are low priorityIndicates issues/PRs that are low priorityarea-System.TransactionsuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
Description
The documentation for System.Transactions.Transaction.Current
states that:
Although you can set the ambient transaction using this property, you should use the TransactionScope object to manipulate the ambient transaction whenever possible.
and then contradicts itself:
This property is thread static. If you change the ambient transaction using this property inside a TransactionScope an InvalidOperationException is thrown when Dispose is called, and the previous ambient transaction value is restored.
And the property is not exactly thread static.
This code (run in LINQPad):
async Task Main()
{
Thread.CurrentThread.ManagedThreadId.Dump("before await");
var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
var transaction = Transaction.Current.Dump();
Transaction.Current = transaction;
Transaction.Current.Dump(); // not null
await Task.Delay(1000).ConfigureAwait(false);
Thread.CurrentThread.ManagedThreadId.Dump("after first await");
//Transaction.Current = transaction;
Transaction.Current.Dump(); // null
await Task.Delay(1000).ConfigureAwait(false);
Thread.CurrentThread.ManagedThreadId.Dump("after second await");
Transaction.Current.Dump(); // null :/
}
doesn't throw but "looses" the transaction as soon as the thread changes.
Metadata
Metadata
Assignees
Labels
Pri3Indicates issues/PRs that are low priorityIndicates issues/PRs that are low priorityarea-System.TransactionsuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner