FileNotFound 加载依赖于另一个域的程序集时 [英] FileNotFound when load assembly with dependency to another domain

查看:22
本文介绍了FileNotFound 加载依赖于另一个域的程序集时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用插件制作应用程序.

I'm trying to make application with plugins.

我有 MainLib.dll,在那里我用 1 个方法制作了一些通用接口(让它成为 ICommon).然后,我制作了 2 个 .dll(插件),它们引用了 MainLib.dll 并在某些类中实现了 ICommon.此外,我删除了此 .dll 中的所有引用,除了 System.

I have MainLib.dll, where I made some commnon interface(let it be ICommon) with 1 method. Then, I made 2 .dlls(plugins) which have reference to MainLib.dll and implement the ICommon in some classes. Also, I removed all the references in this .dlls exept System.

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

Then, I created an application, which monitors folder ".\Plugins" and loads all .dlls in newDomain, check if the types in .dll implement ICommon (so this application also reference to MainLib.dll). If yes - add the name of .dll in some list.

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

And now here the problem: before I tried to load plugins - I load MailLib.dll and System to newDomain because all plugins have dependency of this .dlls. They load correct. Then, I start to load plugins, and here I have:

FileNotFoundException,无法加载文件或程序集PluginWithException,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null"或其依赖项之一.系统找不到指定的文件.) on string Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);

FileNotFoundException, Could not load file or assembly 'PluginWithException, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.) on string Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);

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

PluginWithException assembly has only 2 dependency - System and MainLib. Before I tryied to load PluginWithException I checked assemblies in new domain, System and MainLib were loaded to this domain. So I can't see any ploblems with dependency. I read this topic, and tryed the solution with ProxyDomain but the exception is the same.

我做错了什么?

代码如下:

public static List<string> 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<string> plugins = new List<string>();

    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;
}

推荐答案

您可能想要告诉域从何处加载程序集:

You may want to tell the domain where to load your assemblies from:

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

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

However, I don't see why you are loading assemblies in current (default) domain and also the tmpDomain.

这篇关于FileNotFound 加载依赖于另一个域的程序集时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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