From 728a90773a7d2ebba1d9dec4f8009f9c5e1c024f Mon Sep 17 00:00:00 2001 From: Chris B Date: Tue, 24 May 2022 16:54:05 +0200 Subject: [PATCH] use AssemblyDependencyResolver in OletxCompatibilityLoadContext and NuGet win-x64 runtimes folder --- README.md | 5 ++-- samples/Sample1/Sample1.csproj | 2 +- .../OletxCompatibilityLoadContext.cs | 27 +++++++++++++++---- .../Softwarehelden.Transactions.Oletx.csproj | 25 ++++++++++++----- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1f391fa..dc4de95 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ data providers in .NET Core, applications can use the compatibility assembly loa `OletxCompatibilityLoadContext` from this project to load the data provider in compatibility mode. This library provides types and methods from the `System` namespace that need to be compiled at runtime but are unknown to the .NET Core runtime (e.g. `System.EnterpriseServices.ITransaction`). +The `OletxCompatibilityLoadContext` load context is only supported on `win-x64`. Related .NET issue: https://github.com/dotnet/runtime/issues/715 @@ -148,8 +149,8 @@ The following .NET data providers are supported: | .NET Data Provider | Database | Enlistment | Recovery | Remarks | | -------------------------- | -------------------- | --------------- | -------------------------------------------------- | -------------------------------------------- | -| `Microsoft.Data.SqlClient` | Microsoft SQL Server | PSPE | yes | | -| `System.Data.SqlClient` | Microsoft SQL Server | PSPE | yes | | +| `Microsoft.Data.SqlClient` | Microsoft SQL Server | PSPE | yes | Use `MsSqlPatcher` if `Pooling=True` | +| `System.Data.SqlClient` | Microsoft SQL Server | PSPE | yes | Use `MsSqlPatcher` if `Pooling=True` | | `Oracle.DataAccess` | Oracle Database | PSPE (external) | yes (Oracle MTS Recovery Service) | `UseOraMTSManaged=false` and `CPVersion=1.0` | | `Npgsql` | PostgreSQL | Volatile | [no](https://github.com/npgsql/npgsql/issues/1378) | | diff --git a/samples/Sample1/Sample1.csproj b/samples/Sample1/Sample1.csproj index 0ea3797..df24cad 100644 --- a/samples/Sample1/Sample1.csproj +++ b/samples/Sample1/Sample1.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Softwarehelden.Transactions.Oletx/OletxCompatibilityLoadContext.cs b/src/Softwarehelden.Transactions.Oletx/OletxCompatibilityLoadContext.cs index 5c05687..2748725 100644 --- a/src/Softwarehelden.Transactions.Oletx/OletxCompatibilityLoadContext.cs +++ b/src/Softwarehelden.Transactions.Oletx/OletxCompatibilityLoadContext.cs @@ -13,7 +13,7 @@ public class OletxCompatibilityLoadContext : AssemblyLoadContext { private const string AssemblyPrefix = "Softwarehelden"; - private readonly string assemblyDirectory; + private readonly AssemblyDependencyResolver assemblyDependencyResolver; private readonly Type dbProviderFactoryType; private readonly Type entryType; @@ -22,11 +22,28 @@ public class OletxCompatibilityLoadContext : AssemblyLoadContext /// provider factory type. /// public OletxCompatibilityLoadContext(Type entryType, Type dbProviderFactoryType) + : this(Assembly.GetEntryAssembly(), entryType, dbProviderFactoryType) { - this.entryType = entryType; - this.dbProviderFactoryType = dbProviderFactoryType; + } + + /// + /// Creates a new compatibility assembly load context for the given entry assembly, entry + /// type and database provider factory type. + /// + /// The entry assembly of the current process (e.g. Assembly.GetEntryAssembly()) + /// The entry type for the load context that uses the dbProviderFactoryType + /// The type of the db provider factory (e.g. MyNetFxClientFactory) + public OletxCompatibilityLoadContext(Assembly entryAssembly, Type entryType, Type dbProviderFactoryType) + { + if (entryAssembly == null) + { + throw new ArgumentNullException(nameof(entryAssembly)); + } + + this.assemblyDependencyResolver = new AssemblyDependencyResolver(entryAssembly.Location); - this.assemblyDirectory = Path.GetDirectoryName(this.entryType.Assembly.Location); + this.entryType = entryType ?? throw new ArgumentNullException(nameof(entryType)); + this.dbProviderFactoryType = dbProviderFactoryType ?? throw new ArgumentNullException(nameof(dbProviderFactoryType)); } /// @@ -52,7 +69,7 @@ public T CreateInstance() /// protected override Assembly Load(AssemblyName assemblyName) { - string dependencyAssemblyPath = Path.Combine(this.assemblyDirectory, $"{AssemblyPrefix}.{assemblyName.Name}.dll"); + string dependencyAssemblyPath = this.assemblyDependencyResolver.ResolveAssemblyToPath(new AssemblyName($"{AssemblyPrefix}.{assemblyName.Name}")); if (File.Exists(dependencyAssemblyPath)) { diff --git a/src/Softwarehelden.Transactions.Oletx/Softwarehelden.Transactions.Oletx.csproj b/src/Softwarehelden.Transactions.Oletx/Softwarehelden.Transactions.Oletx.csproj index fe3eb05..89d5bdb 100644 --- a/src/Softwarehelden.Transactions.Oletx/Softwarehelden.Transactions.Oletx.csproj +++ b/src/Softwarehelden.Transactions.Oletx/Softwarehelden.Transactions.Oletx.csproj @@ -13,7 +13,7 @@ git MIT Readme.md - $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage + $(TargetsForTfmSpecificContentInPackage);SetTfmSpecificPackageFiles @@ -29,15 +29,26 @@ - + - - - - + + runtimes/win-x64/lib/$(TargetFramework) + + + runtimes/win-x64/lib/$(TargetFramework) + + + runtimes/win-x64/lib/$(TargetFramework) + + + runtimes/win-x64/lib/$(TargetFramework) + + + runtimes/win-x64/lib/$(TargetFramework) + - +