TransactionScope in async Init
See original GitHub issueDescription
Async context started in TestInitialize method is not flow across thread continuations in Test method
Steps to reproduce
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using System.Transactions;
using System.Threading;
namespace testfail
{
[TestClass]
public class UnitTest1
{
TransactionScope _transactionScope;
AsyncLocal<string> _asyncLocal;
[TestInitialize]
public async Task Init()
{
_transactionScope = new TransactionScope(System.Transactions.TransactionScopeAsyncFlowOption.Enabled);
_asyncLocal = new AsyncLocal<string>();
_asyncLocal.Value = "teststr";
await Task.Delay(100);
Assert.IsNotNull(Transaction.Current, "Transaction.Current in Init is null");
}
[TestCleanup]
public void TestCleanup()
{
_transactionScope?.Dispose();
}
[TestMethod]
public void TestMethodUsesTransactionFromInit()
{
Assert.IsNotNull(Transaction.Current, "Transaction.Current is null");
Assert.AreEqual("teststr", _asyncLocal.Value);
}
[TestMethod]
public async Task TestMethodUsesTransactionCreatedBySelf()
{
using (var localTransactionScope = new TransactionScope(System.Transactions.TransactionScopeAsyncFlowOption.Enabled))
{
Assert.IsNotNull(Transaction.Current, "Transaction.Current before await is null");
await Task.Delay(100);
Assert.IsNotNull(Transaction.Current, "Transaction.Current after await is null");
}
}
}
}
Expected behavior
Test passed because async context flow across thread continuations
Actual behavior
Async context flow only in Init method. Also if I started async operation in TestMethod it’s also worked.
Environment
.NETCoreApp,Version=v3.0 Microsoft ® Test Execution Command Line Tool Version 16.3.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Get TransactionScope to work with async / await
Here is a very simple code to reproduce a TransactionScope error pastebin.com/Eh1dxG4a except that the exception here is Transaction Aborted.
Read more >TransactionScope and Async/Await. Be one with the flow!
TransactionScope class allows you to wrap your database code, your infrastructure code and sometimes even third-party code (if supported by the ...
Read more >TransactionScope and Async/Await. Be one with the flow!
A TransactionScope wrapping asynchronous code needs to specify TransactionScopeAsyncFlowOption.Enabled in its constructor. If you want to learn ...
Read more >Implementing an Implicit Transaction using ...
The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to ...
Read more >How to dispose TransactionScope in cancelable async/await?
A simple solution I think would be to move the creation of the TransactionScope object and the Connection object into the async action....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is really disappointing, particularly because this worked in previous versions of .NET (at least it does on my old, .NET Framework 4.8 app). Is this an issue with .NET Core?
@alexey-gusarov : As a workaround, you can store the value of Transaction.Current in a static member variable and use that member variable instead in your test methods.