Skip to content

Commit 1334210

Browse files
committed
Added PreferPlugin option to UseHostApplicationAssemblies
1 parent 6a20567 commit 1334210

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

src/Weikio.PluginFramework/Context/PluginAssemblyLoadContext.cs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,19 @@ protected override Assembly Load(AssemblyName assemblyName)
5656

5757
if (TryUseHostApplicationAssembly(assemblyName))
5858
{
59-
try
60-
{
61-
var defaultAssembly = Default.LoadFromAssemblyName(assemblyName);
59+
var foundFromHostApplication = LoadHostApplicationAssembly(assemblyName);
6260

61+
if (foundFromHostApplication)
62+
{
6363
Log(LogLevel.Debug, "Assembly {AssemblyName} is available through host application's AssemblyLoadContext. Use it. ", ex: null,
6464
assemblyName);
6565

6666
return null;
6767
}
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);
7472
}
7573

7674
string assemblyPath;
@@ -96,6 +94,19 @@ protected override Assembly Load(AssemblyName assemblyName)
9694
return result;
9795
}
9896

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+
99110
if (_options.AdditionalRuntimePaths?.Any() != true)
100111
{
101112
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)
122133

123134
return null;
124135
}
125-
136+
126137
private bool TryUseHostApplicationAssembly(AssemblyName assemblyName)
127138
{
128139
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)
143154
return true;
144155
}
145156

146-
var name = assemblyName.Name;
157+
if (_options.UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum.Selected)
158+
{
159+
var name = assemblyName.Name;
147160

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;
149162

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);
152165

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;
154178
}
155179

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+
156194
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
157195
{
158196
var nativeHint = _runtimeAssemblyHints.FirstOrDefault(x => x.IsNative && string.Equals(x.FileName, unmanagedDllName));

src/Weikio.PluginFramework/Context/UseHostApplicationAssembliesEnum.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public enum UseHostApplicationAssembliesEnum
1515
/// <summary>
1616
/// Always try to use host application's assemblies
1717
/// </summary>
18-
Always
18+
Always,
19+
20+
/// <summary>
21+
/// Prefer plugin's referenced assemblies, fallback to host application's assemblies
22+
/// </summary>
23+
PreferPlugin
1924
}
2025
}

tests/Assemblies/JsonNetOld/JsonNetOld.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
13-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
12+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1"/>
13+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0"/>
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<ProjectReference Include="..\TestIntefaces\TestIntefaces.csproj" />
17+
<ProjectReference Include="..\TestIntefaces\TestIntefaces.csproj"/>
1818
</ItemGroup>
19-
19+
2020
</Project>

tests/unit/Weikio.PluginFramework.Tests/AssemblyPluginCatalogTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public async Task CanUseHostsDependencies()
176176
}
177177

178178
[Fact]
179-
public async Task CanUseSelectedHoststDependencies()
179+
public async Task CanUseSelectedHostsDependencies()
180180
{
181181
// Make sure that the referenced version of JSON.NET is loaded into memory
182182
var json = Newtonsoft.Json.JsonConvert.SerializeObject(1);

0 commit comments

Comments
 (0)