Skip to content

Commit

Permalink
use AssemblyDependencyResolver in OletxCompatibilityLoadContext and N…
Browse files Browse the repository at this point in the history
…uGet win-x64 runtimes folder (#19)
  • Loading branch information
swh-cb authored May 25, 2022
1 parent 5af1b16 commit 924adf1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) | |

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample1/Sample1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />
<PackageReference Include="Softwarehelden.odp.net.x64" Version="122.19.1" />
<PackageReference Include="Softwarehelden.Transactions.Oletx" Version="1.3.0" />
<PackageReference Include="Softwarehelden.Transactions.Oletx" Version="1.4.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,11 +22,28 @@ public class OletxCompatibilityLoadContext : AssemblyLoadContext
/// provider factory type.
/// </summary>
public OletxCompatibilityLoadContext(Type entryType, Type dbProviderFactoryType)
: this(Assembly.GetEntryAssembly(), entryType, dbProviderFactoryType)
{
this.entryType = entryType;
this.dbProviderFactoryType = dbProviderFactoryType;
}

/// <summary>
/// Creates a new compatibility assembly load context for the given entry assembly, entry
/// type and database provider factory type.
/// </summary>
/// <param name="entryAssembly">The entry assembly of the current process (e.g. Assembly.GetEntryAssembly())</param>
/// <param name="entryType">The entry type for the load context that uses the dbProviderFactoryType</param>
/// <param name="dbProviderFactoryType">The type of the db provider factory (e.g. MyNetFxClientFactory)</param>
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));
}

/// <summary>
Expand All @@ -52,7 +69,7 @@ public T CreateInstance<T>()
/// <inheritdoc/>
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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);SetTfmSpecificPackageFiles</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>

<ItemGroup>
Expand All @@ -29,15 +29,26 @@
<None Include="..\..\Readme.md" Pack="true" PackagePath="/" />
</ItemGroup>

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<Target Name="SetTfmSpecificPackageFiles">
<ItemGroup>
<BuildOutputInPackage Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.mscorlib.dll" />
<BuildOutputInPackage Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.Security.Principal.Windows.dll" />
<BuildOutputInPackage Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.EnterpriseServices.dll" />
<BuildOutputInPackage Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.Data.Entity.dll" />
<TfmSpecificPackageFile Include="bin\$(Configuration)\$(TargetFramework)\Softwarehelden.Transactions.Oletx.dll">
<PackagePath>runtimes/win-x64/lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.mscorlib.dll">
<PackagePath>runtimes/win-x64/lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.Security.Principal.Windows.dll">
<PackagePath>runtimes/win-x64/lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.EnterpriseServices.dll">
<PackagePath>runtimes/win-x64/lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="..\Softwarehelden.Build.Tasks\bin\$(Configuration)\$(TargetFramework)\Softwarehelden.System.Data.Entity.dll">
<PackagePath>runtimes/win-x64/lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
</ItemGroup>
</Target>

<Target Name="SignFileAfterBuild" AfterTargets="Build" Condition="'$(SigningCertificateThumbprint)' != ''">
<SignFile CertificateThumbprint="$(SigningCertificateThumbprint)"
SigningTarget="$(OutputPath)$(AssemblyName).dll"
Expand Down

0 comments on commit 924adf1

Please sign in to comment.