Skip to content

Resolution of native libraries when using DllImport #19929

Open

Description

@antoinebj

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?

Activity

  1. changed the title [-]Resolution of native librarcies when using DllImport[/-] [+]Resolution of native libraries when using DllImport[/+] on Nov 19, 2020
  2. antoinebj commented on Nov 19, 2020

    @antoinebj
    Author

    I also noticed the existence of --additional-deps, but I don't know how to make unit test runners use that parameter.

  3. benvillalobos commented on Nov 25, 2020

    @benvillalobos
    Member
  4. vitek-karas commented on Nov 25, 2020

    @vitek-karas
    Member

    I can only comment on the runtime side of things:

    • If the info is not in .deps.json runtime won't find it - so it's up to SDK to put it there. As you noted this works for PackageReference. I don't know if or how it should for ProjectReference.
    • --additional-deps won't help that much, it lets you specify additional .deps.json, but that assumes you have one. So you would have to "hand-craft" one.
  5. antoinebj commented on Nov 25, 2020

    @antoinebj
    Author

    This has been labeled as a question, which it is indeed, but only because I wouldn't want to make a feature request if there is already an existing solution.
    If the conclusion to this question is that there is no simple solution for what I'm trying to do, then I would be inclined to request an improvement in how .deps.json is generated when PackageReference isn't involved, so as to have the same convenience for loading native libraries.

  6. wasabii commented on Dec 16, 2020

    @wasabii

    I second this. Same issue. Building a .NET Core library that includes some native files (from a VCXPROJ). Works fine when I add it to the produced NuGet package: consumers generate .deps.json files referencing the libraries. However, doesn't work in a Console app within the same solution using ProjectReference, nor in a Unit Test. Though, it should. The only issue is that hte generate .deps.json files for those other projects do not fold in the native assets of the ProjectReference.

    I would expect to be able to simply include Content (or None) item, and perhaps stick <AssetType>native</AssetType> onto it and have it pulled through.

  7. wasabii commented on Dec 16, 2020

    @wasabii

    Digging into it, DependencyContextBuilder just seems to consider NuGet origins. Would need to fix that, as well as the associated GenerateDeps MSBuild task. And then the MSBuild targets to consider these items.

  8. rgebee commented on Jan 27, 2021

    @rgebee

    Adding to this to say I have the same issue with my project Raylib-cs. Took me a while to notice the .deps.json was not including runtimeTargets so for manual installs it would fail to find the library. I am also not sure how to workaround/fix this issue so any suggestions or more information is appreciated.

  9. self-assigned this
    on May 11, 2021
  10. transferred this issue fromdotnet/sdkon Jun 23, 2021
  11. removed their assignment
    on Jun 23, 2021
  12. vitek-karas commented on Jun 25, 2021

    @vitek-karas
    Member

    @marcpopMSFT I think this is very much an SDK issue.

    • SDK needs to communicate information about RID specific assets to the runtime.
    • Such communication channel already exists, it's .deps.json (it's basically the main reason why this file exists today)
    • SDK will correctly populate .deps.json if the RID specific assets are coming from NuGet packages
    • SDK doesn't have mechanisms to define RID specific assets in non-NuGet sources (the project itself, dependent P2P references, ...) - and inherently it doesn't have a mechanism to correctly populate this information into .deps.json
  13. alexrp commented on Jun 26, 2021

    @alexrp
    • SDK doesn't have mechanisms to define RID specific assets in non-NuGet sources (the project itself, dependent P2P references, ...) - and inherently it doesn't have a mechanism to correctly populate this information into .deps.json

    FWIW, having a way to do this is essential for parts of vezel-dev/zig-sdk#8 as well.

  14. marcpopMSFT commented on Jun 28, 2021

    @marcpopMSFT
    Member

    @vitek-karas Isn't this an issue in deps.json creation which from our discussion, the runtime team controls (hence our decision to move it to runtime repo)?

  15. 7 remaining items

  16. ghost added
    untriaged
    on Aug 20, 2021
  17. alexrp commented on Dec 20, 2021

    @alexrp

    Is there a chance that this issue (or something like what @vitek-karas described) might be prioritized for .NET 7?

    Having a way to include RID-specific assets combined with my upcoming work on project/package reference support in Zig.Sdk would be a game-changer for easily adding cross-compiled, in-box native components to any .NET app.

  18. saucecontrol commented on Jan 7, 2022

    @saucecontrol
    Member

    I think the core of the problem is lack of support for RID specific assets our project system

    I think the native binary resolution is just a symptom of a larger problem, which is that when consuming a project that builds a NuGet package as a ProjectReference, none of the package behaviors are brought in. That includes not just RID-specific binaries but also .props and .targets. Perhaps the answer is that things that are expressed by convention (file name/path, etc) in NuGet packages need to be able to be made explicit in the project file so they can be consumed by the SDK. That could also drive the package definition so that less of it is based on magic folder names.

  19. webczat commented on Jan 7, 2022

    @webczat

    just went straight into that issue. how is it to be handled now? build a local nuget just to reference it later?

  20. added this to the Backlog milestone on Jun 13, 2023
  21. marcpopMSFT commented on Jun 13, 2023

    @marcpopMSFT
    Member

    Moving to backlog. This relates to P2P references and native dependencies. Probably could use @richlander doing a pass through this as there isn't a good way to produce packages and projects that have native dependencies in a way that will work well for customers

  22. wasabii commented on Jun 13, 2023

    @wasabii

    We're doing something to solve this in IKVM. But it involves our own P2P implementation, and build time modification of deps.json. so, not great.

  23. alexrp commented on Jun 13, 2023

    @alexrp

    I'd really urge prioritizing this issue. .NET has an excellent interop story these days ... except when it comes to everything build-related. Resolving this issue would bring the story from excellent to amazing. It would also make life massively simpler for those of us who are maintaining MSBuild SDKs that bridge managed and native code.

  24. ero-qt commented on Jan 3, 2025

    @ero-qt

    As a user of the aforementioned Zig.Sdk, I would also like to voice my desire for this issue to receive some priority again.

    Zig.Sdk allows for building C, C++, and Zig code using the .NET CLI. Naturally, I want to pull in some native, header-only libraries, such as boost ornlohmann.json. Today, I need to add a work-around in which I specify the paths to the packages' directories which should be part of the includes.

    If the SDK were able to integrate with PackageReferences better, such a work around would not be necessary.

  25. MiKom commented on Jan 3, 2025

    @MiKom

    This would also be very useful for embedded projects where an end-user application is likely using some DllImport-wrapped C++ libraries to talk to the board. Ideally, I'd like to have the wrappers in separate project and have the proper native libraries nicely copied to runtimes/<rid>/native depending on the target platform.

    Now, I have to do some manual copying as post build steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-NetSDKuntriaged

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions