When reassigning Transaction.Current, I seem to lose the original TransactionScopeAsyncFlowOption behavior of my TransactionScope. The code after the second await loses it's Transaction.Current value.
Sample code (Linqpad):
async Task Main()
{
Thread.CurrentThread.ManagedThreadId.Dump("before await");
var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
var transaction = Transaction.Current;
await Task.Delay(1000).ConfigureAwait(false);
Thread.CurrentThread.ManagedThreadId.Dump("after first await");
Transaction.Current = transaction;
Transaction.Current.Dump(); // not null
await Task.Delay(1000).ConfigureAwait(false);
Thread.CurrentThread.ManagedThreadId.Dump("after second await");
Transaction.Current.Dump(); // is null :/
}
I know I should be using a using statement on TransactionScope rather than reassigning the ambient transaction, however because of reasons, it's not possible to do that. I'm curious about the reason of the behavior of the snippet above and wonder if there are ways to keep the original TransactionScopeAsyncFlowOption behavior.