I have the following code sample:
static void Main(string[] args)
{
TransactionManager.DistributedTransactionStarted += (sender, eventArgs) =>
{
Console.WriteLine("Promoted to distributed transaction!");
};
const string connectionString = @"Server=localhost\SQLEXPRESS;Database=master;Integrated Security=true;";
using (var tx = new TransactionScope())
using (var conn1 = new SqlConnection(connectionString))
using (var conn2 = new SqlConnection(connectionString))
{
conn1.Open();
Console.WriteLine("conn1 opened");
conn2.Open();
Console.WriteLine("conn2 opened");
tx.Complete();
}
Console.ReadLine();
}
When executing this code in a .NET Framework (4.8) Console Application (against SQL Server Express 2017) it yields the following output:
Since the transaction is being promoted to a Distributed Transaction, I expect a similar Console Application targeting .NET Core (3.0) to throw a
System.PlatformNotSupportedException (This platform does not support distributed transactions.).
However, the actual output is:
Why is this? I expect the promotion of a transaction to a distributed transaction to be framework-agnostic.
Edit: This .NET Core (3.0) code sample does something with the database connections:
Database Schema:
CREATE DATABASE [TestDB1]
GO
CREATE TABLE [TestDB1].[dbo].[Table]([Value] [nvarchar](max) NULL)
.NET Core (3.0) Console Application:
static void Main(string[] args)
{
TransactionManager.DistributedTransactionStarted += (sender, eventArgs) =>
{
Console.WriteLine("Promoted to distributed transaction!");
};
const string connectionString = @"Server=localhost\SQLEXPRESS;Database=TestDB1;Integrated Security=true;";
using (var tx = new TransactionScope())
using (var conn1 = new SqlConnection(connectionString))
using (var conn2 = new SqlConnection(connectionString))
{
conn1.Open();
Console.WriteLine("conn1 opened");
using (var cmd1 = conn1.CreateCommand())
{
cmd1.CommandText = "INSERT INTO [dbo].[Table] ([Value]) VALUES ('test 1')";
cmd1.ExecuteNonQuery();
Console.WriteLine("Record inserted through conn1");
}
conn2.Open();
Console.WriteLine("conn2 opened");
using (var cmd2 = conn2.CreateCommand())
{
cmd2.CommandText = "INSERT INTO [dbo].[Table] ([Value]) VALUES ('test 1')";
cmd2.ExecuteNonQuery();
Console.WriteLine("Record inserted through conn2");
}
tx.Complete();
Console.WriteLine("Transaction completed");
}
Console.ReadLine();
}
And the Console output:
Note: This sample also succeeds when using 2 different connectionstrings for the 2 connections!
System.Data.SqlClient
, not withMicrosoft.Data.SqlClient
. All variations produce an exception. – Errolerroll