升级到 .NET 6 后,工作目录中的 dll 的程序集解析失败
我正在将程序从 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能与 .NETCore 中的程序集探测更改有关。例如,
main_process.dll
在其子文件夹中具有依赖项 dllexternaldependency.dll
。如果是这样,您可能需要在
main_process.runtimeconfig.json
中添加additionalAssemblyPaths
,然后在
main_process.deps.json
中添加路径,然后在
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 dllexternaldependency.dll
in its subfolder.If so, you may need to add
additionalAssemblyPaths
inmain_process.runtimeconfig.json
then add path in
main_process.deps.json
Then
main_process
can findexternaldependency.dll
in its subfoldersubfolder_name
in the runtime.Edit:
subfolder_name
could be..
which will look up in the parent folder.