加载程序集时依赖于另一个域的FileNotFound

我正在尝试使用插件进行应用程序。

我有MainLib.dll,我用1方法制作了一些commnon接口(让它是ICommon )。 然后,我制作了2个.dll(插件),它们引用了MainLib.dll并在某些类中实现了ICommon 。 此外,我删除了此.dlls exept System所有引用。

然后,我创建了一个应用程序,它监视文件夹".\\Plugins"并加载newDomain所有.dll,检查.dll中的类型是否实现了ICommon (因此该应用程序也引用了MainLib.dll)。 如果是 – 在某些列表中添加.dll的名称。

现在问题出现了 :在我尝试加载插件之前 – 我将MailLib.dll和System加载到newDomain,因为所有插件都依赖于这个.dll。 他们加载正确。 然后,我开始加载插件,在这里我有:

FileNotFoundException,无法加载文件或程序集’PluginWithException,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null’或其依赖项之一。 系统找不到指定的文件。)字符串上的程序集loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);

PluginWithException程序集只有2个依赖项–System和MainLib。 在我尝试加载PluginWithException之前,我检查了新域中的程序集,System和MainLib已加载到此域。 所以我看不到任何依赖的问题。 我阅读了这个主题,并ProxyDomain使用ProxyDomain解决方案,但exception是相同的。

我做错了什么?

这里的代码:

 public static List SearchPlugins(string[] names) { AppDomain domain = AppDomain.CreateDomain("tmpDomain"); domain.Load(Assembly.LoadFrom(@".\MainLib.dll").FullName); domain.Load(@"System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); MessageBox.Show(GetAssembies(domain)); // here I can see that System and MailLib exist in new domain List plugins = new List(); foreach (string asm in names) { Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName); // here I have exception var theClassTypes = from t in loadedAssembly.GetTypes() where t.IsClass && (t.GetInterface("ICommonInterface") != null) select t; if (theClassTypes.Count() > 0) { plugins.Add(asm); } } AppDomain.Unload(domain); return plugins; } 

您没有指定如何为AppDomains设置搜索路径,以便它可以在Plugins目录中找到DLL,但是您的问题听起来可能与我昨天回答的问题非常类似:

AppDomain.Load()因FileNotFoundException而失败

也许这也会解决你的问题? 让我知道你是怎么办的。

您可能想要告诉域从哪里加载程序集:

AppDomain domain = AppDomain.CreateDomain("tmpDomain", null, new AppDomainSetup { ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins") });

但是,我不明白为什么要在当前(默认)域和tmpDomain中加载程序集。