单文件应用不再在可执行目录中查找本地库

以前,在 单文件 .NET 应用程序中,单文件可执行文件的目录在启动期间已添加到 NATIVE_DLL_SEARCH_DIRECTORIES 属性。 因此,当加载非托管库时,.NET 始终 探测 应用程序目录。 在具有 NativeAOT 的非 Windows 上, rpath 默认设置为应用程序目录,以便它始终在应用程序目录中查找本机库。

应用程序目录不再添加到单文件应用的 NATIVE_DLL_SEARCH_DIRECTORIES 中,并且 rpath 设置已从 NativeAOT 中删除。 在这两种情况下, DllImportSearchPath.AssemblyDirectory (包含在 P/Invokes 的默认行为中)都意味着应用程序目录。 如果指定该值或保留默认值,.NET 将在应用程序目录中查找。 如果指定没有该值的标志,.NET 将不再在应用程序目录中查找。

引入的版本

.NET 10

以前的行为

以前,单文件应用程序在加载本机库时总是在应用程序目录中查找。 在非 Windows 操作系统上,加载本地库时,NativeAOT 应用程序始终在应用程序目录中寻找。

例如,以下 P/Invoke 在应用程序目录中lib查找并加载它(如果存在):

[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()

新行为

从 .NET 10 开始,如果本机库负载的搜索路径指示包括程序集目录,则单文件应用程序只会在应用程序目录中查找。

// Look in System32 on Windows.
// Search the OS on non-Windows.
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()

// Look next to the single-file app because assembly directory
// means application directory for single-file apps.
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
static extern void Method()

// Look next to the single-file app (because assembly
// directory is searched by default), then default OS search.
[DllImport("lib")
static extern void Method()

破坏性变更的类型

这是行为 变化

更改原因

现有行为(即使搜索路径排除它,也始终在应用程序目录中查找)已经导致了混淆。 这与在常规(非单文件、非 NativeAOT).NET 应用程序中处理搜索标志的方式也不一致。

如果 P/Invoke 或本机库加载需要应用程序/程序集目录且之前未指定,请指定 DllImportSearchPath.AssemblyDirectory

RPATH如果 NativeAOT 中需要设置,请显式将相应的链接器参数添加到项目。

受影响的 API

另请参阅