public static IEnumerable <Assembly> FindAssemblies(AssemblyLoadFailure loadFailure, bool includeExeFiles, AssemblyFilter filter) { var assemblyPath = AppDomain.CurrentDomain.BaseDirectory; #if NETCORE var binPath = string.Empty; #else var binPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; #endif if (string.IsNullOrEmpty(binPath)) { return(FindAssemblies(assemblyPath, loadFailure, includeExeFiles, filter)); } if (Path.IsPathRooted(binPath)) { return(FindAssemblies(binPath, loadFailure, includeExeFiles, filter)); } string[] binPaths = binPath.Split(';'); return(binPaths.SelectMany(bin => { var path = Path.Combine(assemblyPath, bin); return FindAssemblies(path, loadFailure, includeExeFiles, filter); })); }
public static IEnumerable<Assembly> FindAssemblies(AssemblyLoadFailure loadFailure, bool includeExeFiles, AssemblyFilter filter) { var assemblyPath = AppDomain.CurrentDomain.BaseDirectory; var binPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; if (string.IsNullOrEmpty(binPath)) { return FindAssemblies(assemblyPath, loadFailure, includeExeFiles, filter); } if (Path.IsPathRooted(binPath)) { return FindAssemblies(binPath, loadFailure, includeExeFiles, filter); } string[] binPaths = binPath.Split(';'); return binPaths.SelectMany(bin => { var path = Path.Combine(assemblyPath, bin); return FindAssemblies(path, loadFailure, includeExeFiles, filter); }); }
public static IEnumerable <Assembly> FindAssemblies(string assemblyPath, AssemblyLoadFailure loadFailure, bool includeExeFiles, AssemblyFilter filter) { LogContext.Debug?.Log("Scanning assembly directory: {Path}", assemblyPath); IEnumerable <string> dllFiles = Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.AllDirectories).ToList(); IEnumerable <string> files = dllFiles; if (includeExeFiles) { IEnumerable <string> exeFiles = Directory.EnumerateFiles(assemblyPath, "*.exe", SearchOption.AllDirectories).ToList(); files = dllFiles.Concat(exeFiles); } foreach (var file in files) { var name = Path.GetFileNameWithoutExtension(file); if (string.IsNullOrWhiteSpace(name)) { continue; } var filterName = Path.GetFileName(file); if (!filter(filterName)) { LogContext.Debug?.Log("Filtered assembly: {File}", file); continue; } Assembly assembly = null; try { assembly = Assembly.ReflectionOnlyLoad(name); } catch (BadImageFormatException exception) { LogContext.Warning?.Log(exception, "Assembly Scan failed: {Name}", name); continue; } catch (Exception originalException) { try { assembly = Assembly.ReflectionOnlyLoad(file); } catch (Exception) { loadFailure(file, originalException); } } if (assembly != null) { Assembly loadedAssembly = null; try { loadedAssembly = Assembly.Load(name); } catch (BadImageFormatException exception) { LogContext.Warning?.Log(exception, "Assembly Scan failed: {Name}", name); continue; } catch (Exception originalException) { try { loadedAssembly = Assembly.Load(file); } catch (Exception) { loadFailure(file, originalException); } } if (loadedAssembly != null) { yield return(loadedAssembly); } } } }
public static IEnumerable<Assembly> FindAssemblies(string assemblyPath, AssemblyLoadFailure loadFailure, bool includeExeFiles, AssemblyFilter filter) { if (_log.IsDebugEnabled) _log.Debug($"Scanning assembly directory: {assemblyPath}"); IEnumerable<string> dllFiles = Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.AllDirectories).ToList(); IEnumerable<string> files = dllFiles; if (includeExeFiles) { IEnumerable<string> exeFiles = Directory.EnumerateFiles(assemblyPath, "*.exe", SearchOption.AllDirectories).ToList(); files = dllFiles.Concat(exeFiles); } foreach (var file in files) { var name = Path.GetFileNameWithoutExtension(file); if (string.IsNullOrWhiteSpace(name)) continue; var filterName = Path.GetFileName(file); if (!filter(filterName)) { if (_log.IsDebugEnabled) _log.Debug($"Filtered assembly: {file}"); continue; } Assembly assembly = null; try { assembly = Assembly.ReflectionOnlyLoad(name); } catch (BadImageFormatException exception) { _log.Debug($"Bad Image Format: {name}", exception); continue; } catch (Exception originalException) { try { assembly = Assembly.ReflectionOnlyLoad(file); } catch (Exception) { loadFailure(file, originalException); } } if (assembly != null) { Assembly loadedAssembly = null; try { loadedAssembly = Assembly.Load(name); } catch (BadImageFormatException exception) { _log.Debug($"Bad Image Format: {name}", exception); continue; } catch (Exception originalException) { try { loadedAssembly = Assembly.Load(file); } catch (Exception) { loadFailure(file, originalException); } } if (loadedAssembly != null) yield return loadedAssembly; } } }
public static IEnumerable <Assembly> FindAssemblies(string assemblyPath, AssemblyLoadFailure loadFailure, bool isPlugIn, AssemblyFilter filter, AssemblyTypeFilter typeFilter) { Log.Information("Scanning assembly directory: {Path}", assemblyPath); IEnumerable <string> dllFiles = Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.AllDirectories).ToList(); IEnumerable <string> files = dllFiles; foreach (var file in files) { var name = Path.GetFileNameWithoutExtension(file); if (string.IsNullOrWhiteSpace(name)) { continue; } var filterName = Path.GetFileName(file); if (!filter(filterName)) { continue; } var depsPath = Path.Combine(assemblyPath, $"{name}.deps.json"); if (!File.Exists(depsPath)) { continue; } Assembly assembly = null; try { if (isPlugIn) { var context = GetAssemblyLoadContext(file, depsPath, true); var defaultAssembly = context.LoadFromAssemblyPath(file); var hasMatchingType = defaultAssembly.GetExportedTypes().Any(x => typeFilter(x)); context.Unload(); if (hasMatchingType) { context = GetAssemblyLoadContext(file, depsPath, false); assembly = context.LoadFromAssemblyPath(file); } } else { assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file); } } catch (BadImageFormatException exception) { loadFailure(file, exception); continue; } catch (FileNotFoundException exception) { loadFailure(file, exception); continue; } if (assembly != null) { yield return(assembly); } } }