Skip to content

Inconsistent documentation on System.Transactions.Transaction.Current #2394

@paulomorgado

Description

@paulomorgado

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

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.TransactionsuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions