简体   繁体   English

C#找不到已加载的程序集

[英]C# can't find assembly which is already loaded

I am writing an application which uses plugins. 我正在编写一个使用插件的应用程序。 Plugins are class libraries which lie in Plug-ins directory. 插件是位于插件目录中的类库。 My app loads these libraries via LoadFrom. 我的应用程序通过LoadFrom加载这些库。 Some of them have dependencies in the form of libraries which lie in the same Plug-ins directory. 其中一些具有库的形式的依赖关系,它们位于相同的插件目录中。 When I try to create instance of class from one of plugins via Activator.CreateInstance i recieve an exception 'Unable to find assembly' (this is dependency assembly of plugin), but this assembly is already loaded (!) along with plugins and It is visible in ProcessExplorer. 当我尝试通过Activator.CreateInstance从其中一个插件创建类的实例时,我收到一个异常'无法找到程序集'(这是插件的依赖程序集),但是这个程序集已经加载(!)和插件,它是在ProcessExplorer中可见。 I can't uderstand in what my trouble is. 我无法理解我的麻烦。

Your problem might be, that de loaded assembly isn't the same version as the request one. 您的问题可能是,de加载的程序集与请求程序集的版本不同 .Net Runtime maps the Assembly after their name and after their Version if the name equals and the Version differes you get an exception if the other one is loaded, which says "Assembly cann't be found" or something like that. .Net运行时将程序集在它们的名称之后和它们的版本之后映射,如果名称等于,并且版本差异,如果加载了另一个,则会得到异常,即“无法找到程序集”或类似的东西。 The Problem is, that the assembly could not be matched properly. 问题是,组件无法正确匹配。 But there is a solution: 但有一个解决方案:
Take a look at the MSDN for further information about that Problem. 请查看MSDN以获取有关该问题的更多信息。

Solution for that problem: 该问题的解决方案:

  1. If you have to load 2 Versions of that assembly try helping the runtime by implementing the AssemblyResolve Event Samples are also here . 如果必须加载组件的2个版本试图通过实施帮助运行时AssemblyResolve事件的样品也是在这里
  2. Try using the AssemblyBindLogViewer to determine the dependencies of your plugins and to crosscheck your problem. 尝试使用AssemblyBindLogViewer来确定插件的依赖关系并交叉检查您的问题。

I recommend implementing the event anyways if you deal with plugins, so you can log all assembly requests of that AppDomain. 如果您处理插件,我建议无论如何都要实现该事件,以便您可以记录该AppDomain的所有程序集请求。

You will find furhter information about runtime behavior and assembly loading here 您将在此处找到有关运行时行为和程序集加载的更多信息

Hope i could help, please give us feedback about your solution! 希望我能提供帮助,请向我们提供有关您解决方案的反馈!

Configuring the Plugin Folder 配置插件文件夹

  1. Load the Plugins into a seperat AppDomain which has the pluginfolder as ApplicationBase 将插件加载到一个单独的AppDomain中该插件具有作为ApplicationBase的pluginfolder
    To configure AppDomains see . 要配置AppDomains, 请参阅 This is the recomendet solution to load Plugins, becaus you can define the security level of the AppDomain (Sandboxing) 这是加载插件的recomendet解决方案 ,因为您可以定义AppDomain的安全级别(沙盒)
  2. Extend your current AppDomains PrivatePath , so it also searches the Assemblies in this Path. 扩展当前的AppDomains PrivatePath ,因此它还会搜索此路径中的程序集。 This method is Obsolete!(but does it's job) 这个方法已经过时了!(但这是它的工作)

You should provide Full Path of assembly files. 您应该提供程序集文件的完整路径。

class Program
{
    static void Main(string[] args)
    {
        var asmFileName = "test.dll"; // Your plug-in file name
        var asmPath = AppDomain.CurrentDomain.BaseDirectory; // Your assemblies's root folder
        var asmFullPath = System.IO.Path.Combine(asmPath, asmFileName);
        var asm = System.Reflection.Assembly.LoadFrom(asmFullPath);
    }
}

我有类似的问题,他们通常通过改变项目属性中的目标框架来解决...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM