目录
[方案一:添加 ARM64 运行时(推荐)](#方案一:添加 ARM64 运行时(推荐))
[方案二:指定目标平台为 x64](#方案二:指定目标平台为 x64)
[方案三:禁用 FaceRecognitionDotNet 的自动部署(临时方案)](#方案三:禁用 FaceRecognitionDotNet 的自动部署(临时方案))
[方案四:手动添加缺失的 DLL](#方案四:手动添加缺失的 DLL)
[方案六:检查并清理 NuGet 包缓存](#方案六:检查并清理 NuGet 包缓存)
这个错误是因为 FaceRecognitionDotNet 包缺少 ARM64 架构的支持文件。这个问题通常出现在你在 ARM64 架构的设备(如 Apple Silicon Mac 或 Windows ARM 设备)上编译项目。
以下是几种解决方案:
方案一:添加 ARM64 运行时(推荐)
在项目文件中添加 ARM64 运行时的回退配置:
xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- 其他配置 -->
<RuntimeIdentifiers>win-x64;win-x86;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<!-- 为 ARM64 添加回退 -->
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture"
Value="X64"
Condition="'$(RuntimeIdentifier)' == 'win-arm64'" />
</ItemGroup>
</Project>
方案二:指定目标平台为 x64
在项目文件中显式指定目标平台:
xml
<PropertyGroup>
<PlatformTarget>x64</PlatformTarget>
<!-- 或者 -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
或者在 Visual Studio 中:
-
右键点击项目 → 属性
-
选择"生成"选项卡
-
将"平台目标"设置为
x64
方案三:禁用 FaceRecognitionDotNet 的自动部署(临时方案)
xml
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- 禁用特定包的自动复制 -->
<ExcludeAssets>runtime</ExcludeAssets>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FaceRecognitionDotNet" Version="1.3.0.7">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
方案四:手动添加缺失的 DLL
如果项目需要支持 ARM64,你可以手动处理:
-
下载或编译 ARM64 版本的 DLL
-
创建目录结构:
text
YourProject/ ├── runtimes/ │ ├── win-arm64/ │ │ └── native/ │ │ └── DlibDotNetNativeDnnAgeClassification.dll │ └── win-x64/ │ └── native/ │ └── (原有的 x64 DLL) -
在项目文件中确保包含这些文件:
xml
<ItemGroup>
<Content Include="runtimes\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
方案五:条件编译(针对不同架构)
xml
<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
<Platforms>AnyCPU;x64;ARM64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)' == 'ARM64'">
<!-- 对于 ARM64 平台,使用 x64 的运行时 -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
方案六:检查并清理 NuGet 包缓存
有时 NuGet 包缓存可能损坏:
-
清理 NuGet 缓存:
powershell
# 清除所有 NuGet 缓存 dotnet nuget locals all --clear -
删除
bin和obj文件夹 -
重新生成项目
快速解决方案
如果你只是想在当前设备上运行,最快速的方法是:
-
在 Visual Studio 中:
-
点击顶部菜单:生成 → 配置管理器
-
将"活动解决方案平台"改为
x64 -
如果没有
x64,点击"新建"创建
-
-
或者修改项目文件:
xml
<PropertyGroup>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
根本原因分析
这个错误是因为 FaceRecognitionDotNet 包(版本 1.3.0.7)不包含 ARM64 架构的原生 DLL 文件。该包只提供了 win-x86 和 win-x64 的运行时。
建议 :如果项目需要支持 ARM64,考虑联系 FaceRecognitionDotNet 包的维护者,请求添加 ARM64 支持,或者寻找替代的库。