dotnet / dotnet/dotnet-api-docs

Inconsistent documentation on System.Transactions.Transaction.Current

Open
#2,394 1 comment 0 reactions 0 assignees View on GitHub
area-System.Transactions Pri3 untriaged
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.