Packaging native DLLs alongside CPP/CLI .NET8 project for consumption in .NET8 C# Project

2 weeks ago 11
ARTICLE AD BOX

I have:
1. A native dll myNativeLib.dll compiled with MSVC (produced via .vcxproj project)
2. A .NET8 targeting CPP/CLI project which produces myInteropLib.dll (produced via .vcxproj project)

I publish the dlls together using a nuspec file:

<?xml version="1.0" encoding="utf-8"?> <package> <metadata> <!-- Redacted --> <version>0.0.1</version> <frameworkAssemblies /> <dependencies> <group targetFramework="net8.0-windows7" /> </dependencies> </metadata> <files> <file target="runtimes/win-x64/native" src="../../../bin/win64/Ijwhost.dll" /> <file target="runtimes/win-x64/native" src="../../../bin/win64/myNativeLib.dll" /> <file target="lib/net8.0-windows7" src="../../../bin/win64/myInteropLib.dll" /> </files> </package>

In a SDK style (.csproj) project I consume this nuget package:

<PackageReference Include="my.company.InterOpLib" Version="0.0.1" />

And I see that the dlls are where I expect them to be

myCSProject/bin/x64/Debug/net8.0-windows7/myInteropLib.dll myCSProject/bin/x64/Debug/net8.0-windows7/runtimes/win-x64/native/Ijwhost.dll myCSProject/bin/x64/Debug/net8.0-windows7/runtimes/win-x64/native/myNativeLib.dll

However when running myCSProject, I get an error that myInteropLib is not found:

OneTimeSetUp: SetUp : System.IO.FileNotFoundException : Could not load file or assembly 'C:\path\to\myCSProject\bin\x64\Debug\net8.0-windows7\myInteropLib.dll'. The specified module could not be found.

I know myInteropLib.dll does exist, but opening it up with Dependencies shows that it in turn cannot find the two native libraries:

Picture of dependencies GUI showing native dlls not found on disk

The .deps.json for myCSProject (and myCSProject.test) shows the native dependencies with the correct paths:

{ "runtimeTarget": { "name": ".NETCoreApp,Version=v8.0", "signature": "" }, "compilationOptions": {}, "targets": { ".NETCoreApp,Version=v8.0": { "MyCSProject.Test/1.0.0": { "dependencies": { "myCSProject": "1.0.0", "Microsoft.NET.Test.Sdk": "18.0.1", "NUnit": "4.5.0", "NUnit.Analyzers": "4.11.2", "NUnit3TestAdapter": "6.1.0", "coverlet.collector": "6.0.4" }, "runtime": { "myCSProject.dll": {} } }, "coverlet.collector/6.0.4": {}, "myInterOpLib/0.0.1": { "runtime": { "lib/net8.0-windows7/myInterOpLib.dll": { "assemblyVersion": "1.0.9523.27495", "fileVersion": "10.5.0.0" } }, "runtimeTargets": { "runtimes/win-x64/native/Ijwhost.dll": { "rid": "win-x64", "assetType": "native", "fileVersion": "8.0.2325.60607" }, "runtimes/win-x64/native/myNativeLib.dll": { "rid": "win-x64", "assetType": "native", "fileVersion": "10.5.0.0" } } },

If I manually copy the native dlls and Ijwhost.dll to the myCSProject/bin/x64/Debug/net8.0-windows7/ directory - the application loads and runs fine. However if I change the target of the nuspec file so the native dlls publish to the lib directory instead of the runtimes/win-x64/native a warning is generated in the downstream project and this documentation indicates the runtimes/win-x64/native directory is correct for native dlls in a nuget package.

What gives?

Read Entire Article