dotnet / dotnet/dotnet-api-docs
Inconsistent documentation on System.Transactions.Transaction.Current
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
The documentation for [`System.Transactions.Transaction.Current`](https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transaction.current?view=netframework-4.8) 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.
Contributor guide
Assessment
This issue has not been assessed yet.