11using Flow . Launcher . Infrastructure ;
22using System ;
3+ using System . Collections . Generic ;
34using System . IO ;
45using System . Linq ;
56using 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