从子文件夹解析程序集的正确方法

Puc*_*acz 3 .net c#

这是我的应用程序文件夹的样子:

Application:
+ App.exe
+ App.exe.config
Application/Plugins:
+ Plugin1 (folder)
Application/Plugins/Plugin1:
+ Plugin1.dll
+ SomeDll.dll
Run Code Online (Sandbox Code Playgroud)

所以主应用程序 App.exe 寻找插件文件夹并将 {PluginName}.dll 加载到内存中以运行它。该插件通常使用它自己的必须加载的依赖程序集(如 SomeDll.dll)。看起来它有时会制造严重的麻烦。我收到异常,例如无法找到依赖程序集的依赖程序集,我不知道为什么。

例如,我的插件必须加载大量额外的 dll,因为插件运行 OwinSelfHost 服务。

所以它必须加载例如:

System.Web.Http.Owin
Owin
Microsoft.Owin
Microsoft.Owin.Host.HttpListener
Microsoft.Owin.Hosting
Run Code Online (Sandbox Code Playgroud)

当加载Microsoft.Owin.Hosting然后抛出无法加载Microsoft.Owin 的异常

异常看起来像:

Could not load file or assembly 'Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of it's dependencies. File not found.
Run Code Online (Sandbox Code Playgroud)

Lar*_*rry 5

我写了这个方法来解析程序集。它经过调整以满足我的需求。

它基本上将AssemblyResolve事件挂钩到当前应用程序域,以从目录列表中检索请求的程序集。

没有简单的方法可以找到与要解析的命名空间匹配的程序集文件的位置,除非加载程序集文件并检查它属于哪个命名空间。

另外,它会丢弃一些不需要的程序集(如序列化程序、资源...)并检测不是 .NET 程序集的 dll 或 exe。

更好的方法是使用全局程序集缓存,但我们希望我们的插件完全可移动。所以在这里。

public static class AssemblyResolver
{
    internal static void Hook(params string[] folders)
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                // Check if the requested assembly is part of the loaded assemblies
                var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
                if (loadedAssembly != null)
                    return loadedAssembly;

                // This resolver is called when an loaded control tries to load a generated XmlSerializer - We need to discard it.
                // http://connect.microsoft.com/VisualStudio/feedback/details/88566/bindingfailure-an-assembly-failed-to-load-while-using-xmlserialization

                var n = new AssemblyName(args.Name);

                if (n.Name.EndsWith(".xmlserializers", StringComparison.OrdinalIgnoreCase))
                    return null;

                // http://stackoverflow.com/questions/4368201/appdomain-currentdomain-assemblyresolve-asking-for-a-appname-resources-assembl

                if (n.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                    return null;

                string assy = null;

                // Find the corresponding assembly file
                foreach (var dir in folders)
                {
                    assy = new[] { "*.dll", "*.exe" }.SelectMany(g => Directory.EnumerateFiles(dir, g)).FirstOrDefault(f =>
                               {
                                   try { return n.Name.Equals(AssemblyName.GetAssemblyName(f).Name, StringComparison.OrdinalIgnoreCase); }
                                   catch (BadImageFormatException) { return false; /* Bypass assembly is not a .net exe */ }
                                   catch (Exception ex) { throw new ApplicationException("Error loading assembly " + f, ex); }
                               });

                    if (assy != null)
                        return Assembly.LoadFrom(assy);
                }

                throw new ApplicationException("Assembly " + args.Name + " not found");
            };
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是它的工作原理:

AssemblyResolver.Hook("\Plugins", "\CommonReferences");
Run Code Online (Sandbox Code Playgroud)

每次需要解析某些程序集时,它都会获取加载到内存中的程序集,否则它将在任何给定文件夹中搜索。