Description
TL;DR: How can I make DllImport
automatically load the right native library from the runtimes/<rid>/native
path when it is in a project referred by ProjectReference
, similar to what PackageReference
enables by adding the appropriate resolution information in deps.json?
--
If I create a NuGet package and put native DLLs in runtimes/<rid>/native/
, the deps.json file generated for the assembly referencing that package will get runtimeTargets
entries for the native libraries, allowing DllImport
to find the appropriate DLLs to load. That mechanism is very useful for easily targeting multiple platforms with "Any CPU" despite reliance on native libraries that are each specific to a platform/architecture combination.
Now I would like to unit-test the classes in that package. Usually I put the unit test project in the same solution, make it refer to the project containing the classes to test. Unfortunately, that doesn't bring the mechanisms from PackageReference
, as it only uses a ProjectReference
. Therefore the deps.json for the unit test assembly doesn't get hints for the native libraries, and when the unit test is run, it complains about not finding the native DLLs.
A workaround would be to have a separate NuGet package containg nothing more than the native libraries, making the project containing .NET code refer to it, similar to what libgit2sharp does. However that's not convenient when updating the native libraries because it requires publishing their package before being able to test their integration in the .NET code.
I know about the NativeLibrary class where one can register a custom resolver, but that seems tedious for true multiplatform. I didn't find a simple way to replicate the RID-based resolution.
Is there any other way? Any thoughts?