Skip to content

Commit 0683640

Browse files
authored
Merge pull request #367 from taooceros/AvoidDuplicateAssemblyLoad
avoid duplicate assembly load
2 parents 374bb79 + 3667805 commit 0683640

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Flow.Launcher.Infrastructure;
22
using System;
3+
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
56
using System.Reflection;
@@ -11,17 +12,23 @@ internal class PluginAssemblyLoader : AssemblyLoadContext
1112
{
1213
private readonly AssemblyDependencyResolver dependencyResolver;
1314

14-
private readonly AssemblyDependencyResolver referencedPluginPackageDependencyResolver;
15-
1615
private readonly AssemblyName assemblyName;
1716

17+
private static readonly List<Assembly> loadedAssembly;
18+
19+
static PluginAssemblyLoader()
20+
{
21+
loadedAssembly = new List<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
22+
AppDomain.CurrentDomain.AssemblyLoad += (sender, args) =>
23+
{
24+
loadedAssembly.Add(args.LoadedAssembly);
25+
};
26+
}
27+
1828
internal PluginAssemblyLoader(string assemblyFilePath)
1929
{
2030
dependencyResolver = new AssemblyDependencyResolver(assemblyFilePath);
2131
assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(assemblyFilePath));
22-
23-
referencedPluginPackageDependencyResolver =
24-
new AssemblyDependencyResolver(Path.Combine(Constant.ProgramDirectory, "Flow.Launcher.Plugin.dll"));
2532
}
2633

2734
internal Assembly LoadAssemblyAndDependencies()
@@ -33,10 +40,10 @@ protected override Assembly Load(AssemblyName assemblyName)
3340
{
3441
string assemblyPath = dependencyResolver.ResolveAssemblyToPath(assemblyName);
3542

36-
// When resolving dependencies, ignore assembly depenedencies that already exits with Flow.Launcher.Plugin
37-
// Otherwise will get unexpected behaviour with plugins, e.g. JsonIgnore attribute not honored in WebSearch or other plugins
38-
// that use Newtonsoft.Json
39-
if (assemblyPath == null || ExistsInReferencedPluginPackage(assemblyName))
43+
// When resolving dependencies, ignore assembly depenedencies that already exits with Flow.Launcher
44+
// Otherwise duplicate assembly will be loaded and some weird behavior will occur, such as WinRT.Runtime.dll
45+
// will fail due to loading multiple versions in process, each with their own static instance of registration state
46+
if (assemblyPath == null || ExistsInReferencedPackage(assemblyName))
4047
return null;
4148

4249
return LoadFromAssemblyPath(assemblyPath);
@@ -45,13 +52,14 @@ protected override Assembly Load(AssemblyName assemblyName)
4552
internal Type FromAssemblyGetTypeOfInterface(Assembly assembly, params Type[] types)
4653
{
4754
var allTypes = assembly.ExportedTypes;
48-
4955
return allTypes.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Intersect(types).Any());
5056
}
5157

52-
internal bool ExistsInReferencedPluginPackage(AssemblyName assemblyName)
58+
internal bool ExistsInReferencedPackage(AssemblyName assemblyName)
5359
{
54-
return referencedPluginPackageDependencyResolver.ResolveAssemblyToPath(assemblyName) != null;
60+
if (loadedAssembly.Any(a => a.FullName == assemblyName.FullName))
61+
return true;
62+
return false;
5563
}
5664
}
57-
}
65+
}

0 commit comments

Comments
 (0)