无法加载文件或程序集或其依赖项之一。该系统找不到指定的文件。 (不允许GAC) [英] Could not load file or assembly or one of its dependencies. The system cannot find the file specified. (No GAC allowed)

查看:181
本文介绍了无法加载文件或程序集或其依赖项之一。该系统找不到指定的文件。 (不允许GAC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主程序,该程序使用反射来动态加载我自己的 plugin.dll文件。通过使用Visual Studio引用, plugin.dll文件引用了第三方 device.dll。只要 device.dll和 plugin.dll与 Main程序位于同一文件夹中,或者 Main程序引用 plugin.dll,一切正常。但是,一旦我将 device.dll文件移动到另一个文件夹,就会收到以下错误消息:
无法加载文件或程序集或其依赖项之一。系统找不到指定的文件。
出现问题是因为程序找不到 device.dll文件。
复制本地属性或引用路径对该问题没有任何影响。我知道我必须指定 device.dll的路径,但是如果不将 plugin.dll和 device.dll放入GAC(全局程序集缓存),我将找不到解决方法。 GAC并不是一种解决方案,因为第三方dll也必须使用强名称(重复组装和重新组装),这是版权许可所不允许的。

I have the "Main" program which loads my own "plugin.dll" file dynamically using reflection. The "plugin.dll" file references to the third party "device.dll" by using visual studio references. As long as the "device.dll" and "plugin.dll" are in the same folder as the "Main" program or the "Main" program has the reference to the "plugin.dll" everything works fine. But as soon as I move the "device.dll" file to another folder I get the following error message: "Could not load file or assembly or one of its dependencies. The system cannot find the file specified." The problem occurs because the program does not find the "device.dll" file. The "Copy Local" property or the "Reference paths" does not have any effect towards this issue. I understand that I have to specify the path to the "device.dll" but I could not find a way to do it without putting the "plugin.dll" and "device.dll" to the GAC (global assembly cache). GAC is not a solution because third party dll has to be strong-named as well (dissasemble and assemble again), which is not allowed by copyright license.

其他详细信息:
使用Visual Studio 2013和.Net Framework 4.0
FUSLOGVW不会注册任何错误(任何想法为什么?)。

Additional details: Using visual studio 2013 and .Net framework 4.0 FUSLOGVW does not register any errors (any ideas why?).

期待您的建议,
预先表示感谢。

Looking forward to your suggestions, thanks in advance.

推荐答案

检查 AppDomain.AssemblyResolve 事件。这是您可以执行自定义操作以将程序集加载到非默认位置的理想点。

Check the AppDomain.AssemblyResolve event. It's the ideal point where you may peform a custom action to load an assembly in a non-default location.

我在此处添加链接的MSDN文章的引用:

I add here a quote of linked MSDN article:


此事件的ResolveEventHandler负责
返回由ResolveEventArgs.Name指定的程序集
属性,如果未识别该程序集,则返回null。
程序集必须加载到执行上下文中;如果将
加载到仅反射上下文中,则导致将该事件引发到
的加载失败。

It is the responsibility of the ResolveEventHandler for this event to return the assembly that is specified by the ResolveEventArgs.Name property, or to return null if the assembly is not recognized. The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

最终,这意味着当无法使用默认程序集发现方法加载程序集时,您的应用程序或服务将进入 AssemblyResolve 事件案例( AppDomain.BaseDirectory 全局程序集缓存 ...)。

At the end of the day, this means that your application or service will enter into the AssemblyResolve event case when an assembly couldn't be loaded using default assembly discovery approach (AppDomain.BaseDirectory, Global Assembly Cache...).

一旦引发了所谓的事件,您需要返回 Assembly 的实例,这是您的工作决定如何加载程序集(从文件,流,字节...)。也就是说,如果您将插件程序集放置在某个子目录中或知道位置,则可以使用正确的完整程序集路径调用 Assembly.Load 来加载和避免程序集加载问题。

Once the so-called event is raised, you need to return an instance of Assembly and it's your job deciding how to load the assembly (from file, stream, bytes...). That is, if you're placing your plugin assemblies in some sub-directory or who knows where, you can call Assembly.Load with the right full assembly path to load them and avoid your assembly loading issues.

这篇关于无法加载文件或程序集或其依赖项之一。该系统找不到指定的文件。 (不允许GAC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆