装配加载并加载“子模块”依赖项 - “找不到指定的文件”
有几个问题提出了同样的问题。然而他们收到的答案我无法理解。类似问题:
简而言之问题:
我需要弄清楚如何动态加载依赖项,即我的模块中的引用。现在,我在所谓的模块中引用的程序集上收到“系统找不到指定的文件”。
我真的无法理解如何使用 AssemblyResolve 事件。
较长的版本
我有一个应用程序,MODULECONTROLLER,它加载单独的模块。
这些“单独的模块”位于众所周知的子目录中,例如
appBinDir\Modules\Module1
appBinDir\Modules\Module2
每个目录都包含构建后这些项目的 bin 目录中存在的所有 DLL。
因此,MODULECONTROLLER 使用以下代码加载这些文件夹中包含的所有 DLL:
byte[] bytes = File.ReadAllBytes(dllFileFullPath);
Assembly assembly = null;
assembly = Assembly.Load(bytes);
如您所见,我正在加载 byte[] 数组(因此我不锁定 DLL 文件)。
现在,例如在 MODULE1 中,我有一个名为 MyGreatXmlProtocol 的静态引用。然后,MyGreatXmlProtocol.dll 也存在于目录 appBinDir\Modules\Module1
中,并使用上面的代码加载
当 MODULE1 中的代码尝试使用此 MyGreatXmlProtocol 时,我得到:
Could not load file or assembly 'MyGreatXmlProtocol, Version=1.0.3797.26527, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
所以,在帖子中(例如这个)他们说
据我了解,反思将 加载主程序集然后搜索 引用程序集的 GAC, 如果在那里找不到它,您可以 然后合并一个 assemblyResolve 事件:
First; 真的需要使用 AssemblyResolve-event 来完成这项工作吗?我的不同模块本身不应该加载它们的 DLL,因为它们是静态引用的?
第二;如果 AssemblyResolve 是可行的方法 - 我该如何使用它?我已将一个处理程序附加到该事件,但我从未在 MyGreatXmlProctol 上得到任何信息。
编辑
有关 AssemblyResolve 事件处理程序的代码:
public GUI()
{
InitializeComponent();
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
...
}
//
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine(args.Name);
return null;
}
There are several questions out there that ask the same question. However the answers they received I cannot understand. Similar questions:
- Dynamically Load Assembly and manually force path to get referenced assemblies ;
- Loading assemblies and its dependencies
The question in short:
I need to figure out how dependencies, ie References in my modules can be loaded dynamically. Right now I am getting "The system cannot find the file specified" on Assemblies referenced in my so called modules.
I cannot really understand how to use the AssemblyResolve event.
The longer version
I have one application, MODULECONTROLLER, that loads separate modules.
These "separate modules" are located in well-known subdirectories, like
appBinDir\Modules\Module1
appBinDir\Modules\Module2
Each directory contains all the DLLs that exists in the bin-directory of those projects after a build.
So the MODULECONTROLLER loads all the DLLs contained in those folders using this code:
byte[] bytes = File.ReadAllBytes(dllFileFullPath);
Assembly assembly = null;
assembly = Assembly.Load(bytes);
I am, as you can see, loading the byte[]-array (so I don't lock the DLL-files).
Now, in for example MODULE1, I have a static reference called MyGreatXmlProtocol. The MyGreatXmlProtocol.dll then also exists in the directory appBinDir\Modules\Module1
and is loaded using the above code
When code in the MODULE1 tries to use this MyGreatXmlProtocol, I get:
Could not load file or assembly 'MyGreatXmlProtocol, Version=1.0.3797.26527, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
So, in a post (like this one) they say that
To my understanding reflection will
load the main assembly and then search
the GAC for the referenced assemblies,
if it cannot find it there, you can
then incorparate an assemblyResolve
event:
First; is it really needed to use the AssemblyResolve-event to make this work? Shouldn't my different MODULEs themself load their DLLs, as they are statically referenced?
Second; if AssemblyResolve is the way to go - how do I use it? I have attached a handler to the Event but I never get anything on MyGreatXmlProctol.
Edit
Code regarding the AssemblyResolve-event handler:
public GUI()
{
InitializeComponent();
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
...
}
//
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine(args.Name);
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试对 MyGreatXmlProtocol 程序集执行 assembly = Assembly.Load(bytes); 。我在某处读到,如果您通过字节数组加载程序集,则必须手动解决依赖关系。
try to do
assembly = Assembly.Load(bytes);
to MyGreatXmlProtocol assembly. I read somewhere that if you load assembly by byte array you have to resolve dependencies manually.