You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TransactionScope.Dispose causes a A root ambient transaction was completed before the nested transaction. The nested transactions should be completed first error
#33726
Open
SpaceOgre opened this issue
May 15, 2024
· 0 comments
We are seeing an intermittent error in EF core when running our integration tests. We have not been able to reproduce it consistently and only see it once in a while in Azure DevOps when doing our PR builds that runs all the tests.
Using xUnit and a WebApplicationFactory we are calling our API that is using EF Core to access a database in a docker container.
Since we don't want tests to change data for other tests we have an attribute that creates a TransactionScope and Disposes it when the test is done (see code for it bellow). For this to work we set this Server.PreserveExecutionContext = true; in the WebApplicationFactory.
Not sure if there is a problem in how HandleTransactionCompleted does things when it is a dispose? or if this is a weird bug caused by a combination of things.
/// <summary>/// Apply this attribute to your test method to automatically create a <see cref="TransactionScope"/>/// that is rolled back when the test is finished./// </summary>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple =false, Inherited =true)]
#pragma warning disable CA1001 // Types that own disposable fields should be disposablepublicsealedclassAutoRollbackAttribute:BeforeAfterTestAttribute
#pragma warning restore CA1001 // Types that own disposable fields should be disposable{privateTransactionScope_scope;/// <summary>/// Gets or sets whether transaction flow across thread continuations is enabled for TransactionScope./// By default transaction flow across thread continuations is enabled./// </summary>publicTransactionScopeAsyncFlowOptionAsyncFlowOption{get;set;}= TransactionScopeAsyncFlowOption.Enabled;/// <summary>/// Gets or sets the isolation level of the transaction./// Default value is <see cref="IsolationLevel"/>.Serializable since we only care for correctness and not performance./// </summary>publicIsolationLevelIsolationLevel{get;set;}= IsolationLevel.Serializable;/// <summary>/// Gets or sets the scope option for the transaction./// Default value is <see cref="TransactionScopeOption"/>.Required./// </summary>publicTransactionScopeOptionScopeOption{get;set;}= TransactionScopeOption.RequiresNew;/// <summary>/// Gets or sets the timeout of the transaction, in milliseconds./// By default, the transaction will not timeout./// </summary>publiclongTimeoutInMS{get;set;}=-1;/// <summary>/// Rolls back the transaction./// </summary>publicoverridevoidAfter(MethodInfomethodUnderTest){
_scope.Dispose();}/// <summary>/// Creates the transaction./// </summary>publicoverridevoidBefore(MethodInfomethodUnderTest){varoptions=new TransactionOptions {IsolationLevel=IsolationLevel};if(TimeoutInMS>0)
options.Timeout = TimeSpan.FromMilliseconds(TimeoutInMS);_scope=new TransactionScope(ScopeOption, options, AsyncFlowOption);}}
Include stack traces
System.InvalidOperationException : A root ambient transaction was completed before the nested transaction. The nested transactions should be completed first.
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.HandleTransactionCompleted(Object sender, TransactionEventArgs e)
at System.Transactions.TransactionStateAborted.EnterState(InternalTransaction tx)
at System.Transactions.Transaction.Rollback()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
at GR.Elevinformation.API.IntegrationTests.AutoRollbackAttribute.After(MethodInfo methodUnderTest) in /home/vsts/work/1/s/tests/GR.Elevinformation.API.IntegrationTests/AutoRollbackAttribute.cs:line 47
File a bug
We are seeing an intermittent error in EF core when running our integration tests. We have not been able to reproduce it consistently and only see it once in a while in Azure DevOps when doing our PR builds that runs all the tests.
Using xUnit and a WebApplicationFactory we are calling our API that is using EF Core to access a database in a docker container.
Since we don't want tests to change data for other tests we have an attribute that creates a TransactionScope and Disposes it when the test is done (see code for it bellow). For this to work we set this
Server.PreserveExecutionContext = true;
in the WebApplicationFactory.Not sure if there is a problem in how HandleTransactionCompleted does things when it is a dispose? or if this is a weird bug caused by a combination of things.
Include your code
The last test that failed:
Autorollback attribute:
Include stack traces
Include provider and version information
EF Core version: 8.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Ubuntu 22.04.4
The text was updated successfully, but these errors were encountered: