Softwarehelden.Transactions.Oletx 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Softwarehelden.Transactions.Oletx --version 1.0.0
NuGet\Install-Package Softwarehelden.Transactions.Oletx -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Softwarehelden.Transactions.Oletx" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Softwarehelden.Transactions.Oletx --version 1.0.0
#r "nuget: Softwarehelden.Transactions.Oletx, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Softwarehelden.Transactions.Oletx as a Cake Addin
#addin nuget:?package=Softwarehelden.Transactions.Oletx&version=1.0.0

// Install Softwarehelden.Transactions.Oletx as a Cake Tool
#tool nuget:?package=Softwarehelden.Transactions.Oletx&version=1.0.0

Distributed OleTx Transactions for MSSQL servers in .NET 6.0

[NuGet]

.NET 6.0 does not support distributed transactions promoted to MSDTC. .NET applications targeting .NET 6.0 can use this library to make promotable transactions work when only Microsoft SQL servers participate in the distributed OleTx transaction.

How it works

System.Transactions throws a PlatformNotSupportedException when a transaction is being promoted to MSDTC using the MSDTC promoter type (TransactionInterop.PromoterTypeDtc). This library patches two methods in System.Transactions to set a custom transaction promoter type for every ADO.NET data provider that supports promotable single phase enlistment (PSPE) using the method Transaction.EnlistPromotableSinglePhase() (e.g. Microsoft.Data.SqlClient). Custom promoter types were introduced in .NET Framework 4.6.1 to support elastic transactions in Azure SQL.

When a transaction is being promoted with a custom promoter type, System.Transactions calls only the Promote method of the promotable single phase notification implemented by the ADO.NET provider and do not call the MSDTC API directly which would result in the PlatformNotSupportedException under .NET 6.0.

The Promote method implemented by ADO.NET data providers usually calls TransactionInterop.GetExportCookie() to propagate/import the transaction over a SQL connection on the target database server. This library replaces the default implementation of TransactionInterop.GetExportCookie() and uses the same MSDTC COM API as the .NET Framework to export a MSDTC transaction cookie that can be used to enlist the promoted transaction on a SQL connection (e.g. TDS propagate request).

The MSDTC transaction cookie that the SQL server can understand and import is created in two steps:

  1. Pull the promoted transaction from the source MSDTC (superior transaction manager) to the local MSDTC (pull propagation)
  2. Push the transaction from the local MSDTC (subordinate transaction manager) to the target MSDTC (push propagation)

Related .NET issue: https://github.com/dotnet/runtime/issues/715

How to use

Call OletxPatcher.Patch() in the entry point of your .NET 6.0 application:

public static class Program
{
    public static async Task Main(string[] args)
    {
        // Patch the OleTx implementation in System.Transactions to support distributed
        // transactions for MSSQL servers under .NET 6.0
        OletxPatcher.Patch();

        // ..
    }
}

Distributed transactions will now work out of the box with MSSQL servers in your application when using the System.Transactions API:

using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled))
{
	using (var sqlConnection = new SqlConnection(connectionString))
	{
		await sqlConnection.OpenAsync(cancellationToken);
		
		using (var command = sqlConnection.CreateCommand())
		{
			command.CommandText = "[CommandText]";
			
			await command.ExecuteNonQueryAsync(cancellationToken);
		}
	}
    
	using (var sqlConnection = new SqlConnection(connectionString2))
	{
		await sqlConnection.OpenAsync(cancellationToken);
		
		using (var command = sqlConnection.CreateCommand())
		{
			command.CommandText = "[CommandText2]";
			
			await command.ExecuteNonQueryAsync(cancellationToken);
		}
	}
	
	transactionScope.Complete();
}

Requirements

  • Only Windows platform is supported (MSDTC COM API is only availabe under Windows)
  • MSDTC must still be installed and available on the application server
  • .NET Framework must be installed on the application server (System.Transaction.dll is used to query the MSDTC COM API)
  • ADO.NET data providers Microsoft.Data.SqlClient and System.Data.SqlClient are supported and tested (Microsoft SQL servers)

Credits

This project uses the Harmony library for patching the System.Transactions methods.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.0 151 4/3/2024
1.4.0 16,026 5/27/2022
1.3.0 576 4/25/2022
1.2.0 494 4/20/2022
1.1.0 468 4/19/2022
1.0.0 485 4/5/2022