A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Fixed a bug where extensions without DevToys.API wouldn't be loaded.
Etienne Baudouxa2fce585929b
diff --git a/src/app/dev/DevToys.Core/Mef/MefComposer.cs b/src/app/dev/DevToys.Core/Mef/MefComposer.csindex dd5608f59..dcef1d50a 100644--- a/src/app/dev/DevToys.Core/Mef/MefComposer.cs+++ b/src/app/dev/DevToys.Core/Mef/MefComposer.cs@@ -12,6 +12,7 @@ public sealed partial class MefComposer : IDisposablepublic const string ExtraPluginEnvironmentVariableName = "EXTRAPLUGIN";private readonly ILogger _logger;+ private readonly Dictionary<string, Assembly> _loadedAssemblies = new();private readonly Assembly[] _assemblies;private readonly string[]? _pluginFolders;private readonly string? _extraPlugin;@@ -26,6 +27,9 @@ public sealed partial class MefComposer : IDisposable{_logger = this.Log();+ AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentAppDomain_AssemblyResolve;+ AppDomain.CurrentDomain.AssemblyResolve += CurrentAppDomain_AssemblyResolve;+_extraPlugin = Environment.GetEnvironmentVariable(ExtraPluginEnvironmentVariableName);_assemblies = assemblies ?? Array.Empty<Assembly>();@@ -37,8 +41,43 @@ public sealed partial class MefComposer : IDisposable((MefProvider)Provider).ExportProvider = ExportProvider;}+ private Assembly? CurrentAppDomain_AssemblyResolve(object? sender, ResolveEventArgs args)+ {+ // Try to resolve the assembly from the list loaded assemblies.+ // We need to do this because when we load an assembly from MEF, the assembly may reference other assemblies+ // that isn't part of the folder where the assembly is, such as "DevToys.API" (extensions generally don't have it in their folder).++ string searchedAssemblyName = new AssemblyName(args.Name).Name!;++ if (_loadedAssemblies.TryGetValue(searchedAssemblyName, out Assembly? assembly))+ {+ return assembly;+ }++ Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();++ for (int i = 0; i < assemblies.Length; i++)+ {+ assembly = assemblies[i];+ string? assemblyName = assembly.GetName().Name;++ if (!string.IsNullOrEmpty(assemblyName))+ {+ _loadedAssemblies[assemblyName] = assembly;+ if (string.Equals(assemblyName, searchedAssemblyName, StringComparison.CurrentCulture))+ {+ return assembly;+ }+ }+ }++ return null;+ }+public void Dispose(){+ AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= CurrentAppDomain_AssemblyResolve;+ AppDomain.CurrentDomain.AssemblyResolve -= CurrentAppDomain_AssemblyResolve;ExportProvider?.Dispose();_isExportProviderDisposed = true;diff --git a/src/app/dev/DevToys.Core/Mef/RecursiveDirectoryCatalog.cs b/src/app/dev/DevToys.Core/Mef/RecursiveDirectoryCatalog.csindex 18f94cf28..2fe1eb7c3 100644--- a/src/app/dev/DevToys.Core/Mef/RecursiveDirectoryCatalog.cs+++ b/src/app/dev/DevToys.Core/Mef/RecursiveDirectoryCatalog.cs@@ -95,28 +95,52 @@ internal sealed partial class RecursiveDirectoryCatalog : ComposablePartCatalog,Changing?.Invoke(o, e);};- foreach (string file in files)+ string pluginFolderName = Path.GetFileName(path);+ string hypotheticalPluginMainDllFile = $"{pluginFolderName}.dll";+ bool foundMefPartInMainDllFile = false;+ string? pluginMainDllFile = files.FirstOrDefault(f => f.EndsWith(hypotheticalPluginMainDllFile, StringComparison.OrdinalIgnoreCase));+ if (!string.IsNullOrEmpty(pluginMainDllFile) && File.Exists(pluginMainDllFile)){- try- {- AssemblyCount++;- var asmCat = new AssemblyCatalog(file);-- // Force MEF to load the plugin and figure out if there are any exports- // good assemblies will not throw the RTLE exception and can be added to the catalog- if (asmCat.Parts.Any())- {- _aggregateCatalog.Catalogs.Add(asmCat);- }- }- catch (ReflectionTypeLoadException ex)+ // Perf: If the main plugin DLL is found, try to load it first.+ // This is to avoid loading all the other DLLs in the directory, which slows down the app's startup.+ foundMefPartInMainDllFile = AddLibraryToMefCatalog(pluginMainDllFile);+ }++ if (!foundMefPartInMainDllFile)+ {+ foreach (string file in files){- LogLoadLibraryFault(ex, file);+ AddLibraryToMefCatalog(file);}- catch (BadImageFormatException)+ }+ }++ private bool AddLibraryToMefCatalog(string filePath)+ {+ Guard.IsNotNull(_aggregateCatalog);++ try+ {+ AssemblyCount++;+ var asmCat = new AssemblyCatalog(filePath);++ // Force MEF to load the plugin and figure out if there are any exports+ // good assemblies will not throw the RTLE exception and can be added to the catalog+ if (asmCat.Parts.Any()){+ _aggregateCatalog.Catalogs.Add(asmCat);+ return true;}}+ catch (ReflectionTypeLoadException ex)+ {+ LogLoadLibraryFault(ex, filePath);+ }+ catch (BadImageFormatException)+ {+ }++ return false;}private string GetDisplayName()