Skip to content

Instantly share code, notes, and snippets.

@SzymonPobiega
Last active May 10, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SzymonPobiega/4cea938078497529059d09953c2cf17b to your computer and use it in GitHub Desktop.
Save SzymonPobiega/4cea938078497529059d09953c2cf17b to your computer and use it in GitHub Desktop.
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