升级到 .NET 6 后,工作目录中的 dll 的程序集解析失败

发布于 2025-01-16 04:49:04 字数 1237 浏览 4 评论 0原文

我正在将程序从 .NET Framework 升级到 .NET 6,并偶然发现程序集加载问题。

该程序是基于插件的,插件 dll 驻留在主文件夹的子文件夹中(主文件夹是程序的工作目录)。 “核心”项目和外部依赖项位于主文件夹中。

主文件夹如下所示:

|-- Program.exe
|-- Utilities.dll
|-- ExternalDependency.dll
|-- plugins
    |-- Plugin.dll

这里,ExternalDependency.dll 是Plugin.dll 的依赖项。这些文件由构建后脚本复制到此文件夹结构中。

启动 Program.exe 时,程序会加载“plugins”文件夹中的所有 dll。加载 Plugin.dll 时,程序无法找到ExternalDependency.dll。

我希望程序在当前工作目录中查找 dll。我已经确认工作目录确实是包含ExternalDependency.dll的目录。

如果我将ExternalDependency.dll 放在插件文件夹中,它确实可以正确加载。我不想这样做,因为在这种情况下我必须将所有内容都放在同一个文件夹中(核心项目、插件和外部依赖项),这会扰乱插件加载。

我还可以附加我自己的方法来解析程序集:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

在这种情况下,我可以加载程序集,甚至只需调用

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    return Assembly.LoadFrom(args.Name.Split(',')[0] + ".dll")
}

,我想,这只是在当前工作目录中查找。

即便如此,实现我自己的程序集解析逻辑来在工作目录中定位 dll 似乎并不正确。此方法似乎也与其他“失败”挂钩,特别是对于特定于区域设置的资源程序集,这些资源程序集可能通过其他一些后备机制而成功。

这曾经在 .NET Framework 上工作,我无法想象为什么它在 .NET 6 上不起作用。当前工作目录不是寻找程序集的明显位置吗?是否有一些配置文件可以控制这个?我还缺少其他东西吗?

编辑:我应该注意到,大多数依赖项(其中几个遵循相同的位置模式)都已正确加载,因此这似乎只是一个实例的问题。

I am upgrading a program from .NET Framework to .NET 6 and have stumbled upon an issue with assembly loading.

The program is plugin-based with plugin dlls residing in a subfolder of the main folder (the main folder is the working directory of the program). The "core" projects and external dependencies reside in the main folder.

The main folder looks like this:

|-- Program.exe
|-- Utilities.dll
|-- ExternalDependency.dll
|-- plugins
    |-- Plugin.dll

Here, ExternalDependency.dll is a dependency of Plugin.dll. The files are copied into this folder structure by a post-build script.

On starting Program.exe, the program loads all dlls in the "plugins" folder. On loading Plugin.dll, the program fails to locate ExternalDependency.dll.

I expect the program to look in the current working directory for the dll. I have confirmed that the working directory is indeed the directory containing ExternalDependency.dll.

If I place ExternalDependency.dll in the plugins folder, it does indeed load correctly. I don't want to to this as I would have to place everything in the same folder in that case (core projects, plugins and external dependencies), which would mess up plugin loading .

I can also attach my own method to resolve the assembly:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

In this case, I am able to load the assembly, even just by calling

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    return Assembly.LoadFrom(args.Name.Split(',')[0] + ".dll")
}

, which, I suppose, just looks in the current working directory.

Even so, it doesn't seem right to implement my own assembly resolution logic to locate a dll in the working directory. This method seems to hook onto other "failures" as well, specifically with locale-specific resource assemblies that otherwise succeed, probably by some other fallback mechanism.

This used to work on .NET Framework and I can't imagine why it would not work on .NET 6. Is the current working directory not an obvious place to look for assemblies? Is there some configuration file somewhere that controls this? Am I missing something else?

Edit: I should note that most dependencies (several of which follow the same location pattern) are loaded correctly, so this seems to be a problem with just one instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-23 04:49:04

这可能与 .NETCore 中的程序集探测更改有关。例如,main_process.dll 在其子文件夹中具有依赖项 dll externaldependency.dll

如果是这样,您可能需要在 main_process.runtimeconfig.json 中添加 additionalAssemblyPaths

"additionalAssemblyPaths" : [
      "subfolder_name"
    ],

,然后在 main_process.deps.json 中添加路径

  "libraries": {
    "externaldependency": {
      "path": "."
    },

,然后在 main_process可以在运行时的子文件夹subfolder_name中找到externaldependency.dll


编辑:
subfolder_name 可以是 .. ,它将在父文件夹中查找。

This probably is related to assembly probing change in .NETCore. For example, main_process.dll has a dependency dll externaldependency.dll in its subfolder.

If so, you may need to add additionalAssemblyPaths in main_process.runtimeconfig.json

"additionalAssemblyPaths" : [
      "subfolder_name"
    ],

then add path in main_process.deps.json

  "libraries": {
    "externaldependency": {
      "path": "."
    },

Then main_process can find externaldependency.dll in its subfolder subfolder_name in the runtime.


Edit:
subfolder_name could be .. which will look up in the parent folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文