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

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

问题描述

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

I'm trying to make application with plugins.

我有MainLib.dll,在其中我用1种方法制作了一些通讯接口(让它为ICommon).然后,我制作了2个.dlls(插件),它们引用了MainLib.dll并在某些类中实现了ICommon.另外,我删除了此.dlls部门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,因为所有插件都具有此.dlls的依赖性.他们加载正确.然后,我开始加载插件,这里有:

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,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.系统找不到指定的文件.)字符串Assembly AssemblyAssembly = 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;
}

推荐答案

您没有指定如何设置AppDomain的搜索路径,以便它可以在Plugins目录中找到DLL,但是听起来很麻烦.就像它可能与我昨天回答的那个非常相似:

you don't specify how you're setting up your search paths for your AppDomains so that it can find the DLLs in the Plugins directory, however your problem sounds like it might be very similar to this one I answered yesterday:

AppDomain.Load()失败,出现FileNotFoundException

也许这也可以解决您的问题?让我知道你过得怎么样.

Perhaps this also will resolve your issue? Let me know how you get on.

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

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