我的项目目前在 .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:
森托斯:
所以我将创建一个 ubuntu build boks 来快速解决这个问题
关于.net - 来自 NuGet 包的 DllImport native 依赖项在 net472 上不起作用,但在 netcoreapp2.2 上有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678984/