@@ -56,21 +56,19 @@ protected override Assembly Load(AssemblyName assemblyName)
56
56
57
57
if ( TryUseHostApplicationAssembly ( assemblyName ) )
58
58
{
59
- try
60
- {
61
- var defaultAssembly = Default . LoadFromAssemblyName ( assemblyName ) ;
59
+ var foundFromHostApplication = LoadHostApplicationAssembly ( assemblyName ) ;
62
60
61
+ if ( foundFromHostApplication )
62
+ {
63
63
Log ( LogLevel . Debug , "Assembly {AssemblyName} is available through host application's AssemblyLoadContext. Use it. " , ex : null ,
64
64
assemblyName ) ;
65
65
66
66
return null ;
67
67
}
68
- catch
69
- {
70
- Log ( LogLevel . Debug ,
71
- "Host application's AssemblyLoadContext doesn't contain {AssemblyName}. Try to resolve it through the plugin's references." , ex : null ,
72
- assemblyName ) ;
73
- }
68
+
69
+ Log ( LogLevel . Debug ,
70
+ "Host application's AssemblyLoadContext doesn't contain {AssemblyName}. Try to resolve it through the plugin's references." , ex : null ,
71
+ assemblyName ) ;
74
72
}
75
73
76
74
string assemblyPath ;
@@ -96,6 +94,19 @@ protected override Assembly Load(AssemblyName assemblyName)
96
94
return result ;
97
95
}
98
96
97
+ if ( _options . UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum . PreferPlugin )
98
+ {
99
+ var foundFromHostApplication = LoadHostApplicationAssembly ( assemblyName ) ;
100
+
101
+ if ( foundFromHostApplication )
102
+ {
103
+ Log ( LogLevel . Debug , "Assembly {AssemblyName} not available from plugin's references but is available through host application's AssemblyLoadContext. Use it. " , ex : null ,
104
+ assemblyName ) ;
105
+
106
+ return null ;
107
+ }
108
+ }
109
+
99
110
if ( _options . AdditionalRuntimePaths ? . Any ( ) != true )
100
111
{
101
112
Log ( LogLevel . Warning , "Couldn't locate assembly using {AssemblyName}. Please try adding AdditionalRuntimePaths using " + nameof ( PluginLoadContextOptions . Defaults . AdditionalRuntimePaths ) , ex : null , assemblyName ) ;
@@ -122,7 +133,7 @@ protected override Assembly Load(AssemblyName assemblyName)
122
133
123
134
return null ;
124
135
}
125
-
136
+
126
137
private bool TryUseHostApplicationAssembly ( AssemblyName assemblyName )
127
138
{
128
139
Log ( LogLevel . Debug , "Determining if {AssemblyName} should be loaded from host application's or from plugin's AssemblyLoadContext" ,
@@ -143,16 +154,43 @@ private bool TryUseHostApplicationAssembly(AssemblyName assemblyName)
143
154
return true ;
144
155
}
145
156
146
- var name = assemblyName . Name ;
157
+ if ( _options . UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum . Selected )
158
+ {
159
+ var name = assemblyName . Name ;
147
160
148
- var result = _options . HostApplicationAssemblies ? . Any ( x => string . Equals ( x . Name , name , StringComparison . InvariantCultureIgnoreCase ) ) == true ;
161
+ var result = _options . HostApplicationAssemblies ? . Any ( x => string . Equals ( x . Name , name , StringComparison . InvariantCultureIgnoreCase ) ) == true ;
149
162
150
- Log ( LogLevel . Debug , "UseHostApplicationAssemblies is set to Selected. {AssemblyName} listed in the HostApplicationAssemblies: {Result}" , ex : null ,
151
- assemblyName , result ) ;
163
+ Log ( LogLevel . Debug , "UseHostApplicationAssemblies is set to Selected. {AssemblyName} listed in the HostApplicationAssemblies: {Result}" , ex : null ,
164
+ assemblyName , result ) ;
152
165
153
- return result ;
166
+ return result ;
167
+ }
168
+
169
+ if ( _options . UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum . PreferPlugin )
170
+ {
171
+ Log ( LogLevel . Debug , "UseHostApplicationAssemblies is set to PreferPlugin. Try to load assembly from plugin's AssemblyLoadContext and fallback to host application's AssemblyLoadContext" ,
172
+ args : assemblyName ) ;
173
+
174
+ return false ;
175
+ }
176
+
177
+ return false ;
154
178
}
155
179
180
+ private bool LoadHostApplicationAssembly ( AssemblyName assemblyName )
181
+ {
182
+ try
183
+ {
184
+ Default . LoadFromAssemblyName ( assemblyName ) ;
185
+
186
+ return true ;
187
+ }
188
+ catch
189
+ {
190
+ return false ;
191
+ }
192
+ }
193
+
156
194
protected override IntPtr LoadUnmanagedDll ( string unmanagedDllName )
157
195
{
158
196
var nativeHint = _runtimeAssemblyHints . FirstOrDefault ( x => x . IsNative && string . Equals ( x . FileName , unmanagedDllName ) ) ;
0 commit comments