Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to TransactionScope of System.Transaction assembly in .net core framework

The System.Transaction assembly is not part of the .net core framework at the moment (see https://github.com/dotnet/corefx/issues/2949). In my application (asp.net core mvc) I need to use TransactionScope for transaction handling.

Question:

Is there an alternative transaction handling which works with the .net core framework? I have tried to use Castle.Transactions as an alternative, which is also not supported at the moment.

like image 690
MUG4N Avatar asked Feb 28 '16 18:02

MUG4N


People also ask

What are the different phases of transaction in C#?

Transaction Control: There are the following commands used to control transactions. COMMIT To save changes. ROLLBACK To rollback the changes. SET TRANSACTION Places a name on a transaction.

How do I rollback a transaction scope?

If you want to rollback a transaction, you should not call the Complete method within the transaction scope. For example, you can throw an exception within the scope. The transaction in which it participates in will be rolled back.

What is ambient transaction C#?

Ambient Transaction If you see the use of TransactionScope, you will not find transaction related anything sent to any method or setting any property. A code block is automatically attached with the transaction if that code is in any TransactionScope.


2 Answers

Update 2 .NET Core 2.0 is out now. You can use this API. See https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope?view=netcore-2.0

Update System.Transactions will be available in NET Core 2.0. See https://github.com/dotnet/core/blob/master/roadmap.md for details on upcoming releases.

Original answer

System.Transactions (or ambient transactions) is not implemented in .NET Core 1.0.0 but may be implemented in future versions.

You can work around this by using explicit transactions.

        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (var transaction = connection.BeginTransaction())
            {
               // transaction.Commit();
               // transaction.Rollback();
            }
        }
like image 166
natemcmaster Avatar answered Sep 18 '22 08:09

natemcmaster


Just wanted to leave a comment here in case anybody wandered by this post while researching this issue. I ran into this randomly on .net core 2.1 using Dapper, which includes System.Data.SqlClient 4.5.0 by default. Adding version System.Data.SqlClient 4.5.1 independently via nuget worked. So it seems this has been fixed as of 4.5.1.

The following are the relevant sections of my csproj file:

<ItemGroup>
   <PackageReference Include="Dapper" Version="1.50.5" />
   <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
like image 36
Brandon Avatar answered Sep 19 '22 08:09

Brandon