question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TransactionScope in async Init

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajbeavencommented, Mar 18, 2021

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?

1reaction
jayaranigargcommented, Nov 28, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found