Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async transaction flow problem
using System.Threading.Tasks;
using System.Transactions;
using NUnit.Framework;
[TestFixture]
public class TransactionFlowTest
{
[Test]
public void It_should_propagate_transaction_current_down_the_stack_sync()
{
Assert.IsNull(Transaction.Current);
using (var scope = CreateScopeSync())
{
Assert.IsNotNull(scope);
Assert.IsNotNull(Transaction.Current);
}
Assert.IsNull(Transaction.Current);
}
static TransactionScope CreateScopeSync()
{
return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions());
}
[Test]
public async Task It_should_propagate_transaction_current_down_the_stack_async()
{
Assert.IsNull(Transaction.Current);
using (var scope = await CreateScopeAsync().ConfigureAwait(false))
{
Assert.IsNotNull(scope);
Assert.IsNotNull(Transaction.Current);
}
Assert.IsNull(Transaction.Current);
}
static async Task<TransactionScope> CreateScopeAsync()
{
await Task.Yield();
return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions(), TransactionScopeAsyncFlowOption.Enabled);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment