.net - 来自 NuGet 包的 DllImport native 依赖项在 net472 上不起作用,但在 netcoreapp2.2 上有效

标签 .net nuget pinvoke

我的项目目前在 .NET Core (Linux/OSX/Windows/netcoreapp2.2) 上完美运行。它应该适用于 net472也是,但出于某种原因,DllImport没有找到 native 依赖项。

可以找到一个简单的重现 here ,但我会在这个问题中进一步扩展以供后代使用。

这是包含 native 依赖项的打包项目。

<Project Sdk="Microsoft.NET.Sdk">
  <Import Project="../../build/common.props" />
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
    <EnableDefaultItems>false</EnableDefaultItems>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <IncludeBuildOutput>false</IncludeBuildOutput>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\net\Qml.Net\Qml.Net.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="output/**/*">
      <PackagePath>runtimes/win-x64/native/</PackagePath>
      <Pack>true</Pack>
    </Content>
  </ItemGroup>
</Project>

这将生成具有以下输出的 NuGet 包。
├── [Content_Types].xml
├── package
│   └── services
│       └── metadata
│           └── core-properties
│               └── e803485f4a674e8d9d0155224fa9cbc2.psmdcp
├── Qml.Net.WindowsBinaries.nuspec
├── _rels
└── runtimes
    └── win-x64
        └── native
            └── QmlNet.dll

8 directories, 4 files

这是我的消费项目。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="**\*.qmlc" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Qml.Net" Version="0.9.0-alpha.5" />
    <PackageReference Include="Qml.Net.WindowsBinaries" Version="0.9.0-alpha.5" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="images\**" CopyToPublishDirectory="Always" />
    <Content Include="pages\**" CopyToPublishDirectory="Always" />
    <Content Include="Main.qml" CopyToPublishDirectory="Always" />
  </ItemGroup>
</Project>

这是尝试执行 PInvoke 的简单代码。

using System;
using System.Runtime.InteropServices;
using Qml.Net.Runtimes;

namespace Features
{
    class Program
    {
        // NOTE: This works if I provide absolute path to QmlNet.dll
        [DllImport("QmlNet")]
        internal static extern long qml_net_getVersion();

        static int Main(string[] args)
        {
            // This is need to prep Qt runtime that QmlNet.dll depends on.
            // It is required, but it is irrelvant to the issue we are experiencing.
            RuntimeManager.DiscoverOrDownloadSuitableQtRuntime();

            // The following works on netcoreapp2.2, but not on 4.7.2.
            Console.WriteLine(qml_net_getVersion());

            return 0;
        }
    }
}


当我运行它时,我得到:
System.DllNotFoundException: 'Unable to load DLL 'QmlNet': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

我尝试将显式目标添加到 net472在我的 NuGet 包中,没有用。

当我的消费项目以 netcoreapp2.2 为目标时,我没有收到任何错误.

如何使用 DllImport 让我的本地依赖项成为属性定位 net472 时?

最佳答案

This answer帮助我从我的 nuget 包中加载我的 native 文件。我唯一额外做的就是像这样使用当前目录

private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
    IntPtr libHandle = IntPtr.Zero;
    //TODO: Add check for windows if needed.
    if (libraryName == WrapperFFI && RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Environment.Is64BitOperatingSystem)
    {
        var res = NativeLibrary.TryLoad(System.IO.Directory.GetCurrentDirectory()+"/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native/libwrapper_ffi.so", out libHandle);
        if (!res) {
            res = NativeLibrary.TryLoad(System.IO.Directory.GetCurrentDirectory()+"/bin/Release/netcoreapp3.1/runtimes/linux-x64/native/libwrapper_ffi.so", out libHandle);
        }
    } 
    return libHandle;
}

编辑:

尽管上述修复对大多数人都有效,但当我拉出基于私有(private)仓库构建的 nuget 包时,它也不起作用。私有(private)仓库的 native 库建立在 centos 上,这导致二进制文件更小,但我仍然无法在我的 ubuntu 开发机器上加载异常。 ldd 显示的 centos 库比建立在 ubuntu 上的依赖项更多

Ubuntu:

enter image description here

森托斯:

enter image description here

所以我将创建一个 ubuntu build boks 来快速解决这个问题

关于.net - 来自 NuGet 包的 DllImport native 依赖项在 net472 上不起作用,但在 netcoreapp2.2 上有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678984/

相关文章:

c# - 发布引用其他程序集的 nuget 包

c# - 从 C# 调用 .DLL 的奇怪问题

c++ - 从 C++ 函数返回字符串到 VB .Net

c# - 以编程方式刷新系统托盘图标

.net - ConfigureAwait(false) 与 ASP.NET Core 相关吗?

c# - 如何将 Image 类型转换为 BitmapImage 类型?

c# - 如何安装 OxyPlot NuGet 包?

nuget - 有没有办法在项目文件上运行 nuget pack 并自动获取依赖项?

c# - 通用 Windows 平台上的 Type.GetTypeCode

.net - 在 .NET 中将文本文件的内容读取为字符串的最佳方法是什么?