Skip to content

Commit

Permalink
Added PreferPlugin option to UseHostApplicationAssemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoskinen committed Sep 21, 2021
1 parent 6a20567 commit 1334210
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
68 changes: 53 additions & 15 deletions src/Weikio.PluginFramework/Context/PluginAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,19 @@ protected override Assembly Load(AssemblyName assemblyName)

if (TryUseHostApplicationAssembly(assemblyName))
{
try
{
var defaultAssembly = Default.LoadFromAssemblyName(assemblyName);
var foundFromHostApplication = LoadHostApplicationAssembly(assemblyName);

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

return null;
}
catch
{
Log(LogLevel.Debug,
"Host application's AssemblyLoadContext doesn't contain {AssemblyName}. Try to resolve it through the plugin's references.", ex: null,
assemblyName);
}

Log(LogLevel.Debug,
"Host application's AssemblyLoadContext doesn't contain {AssemblyName}. Try to resolve it through the plugin's references.", ex: null,
assemblyName);
}

string assemblyPath;
Expand All @@ -96,6 +94,19 @@ protected override Assembly Load(AssemblyName assemblyName)
return result;
}

if (_options.UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum.PreferPlugin)
{
var foundFromHostApplication = LoadHostApplicationAssembly(assemblyName);

if (foundFromHostApplication)
{
Log(LogLevel.Debug, "Assembly {AssemblyName} not available from plugin's references but is available through host application's AssemblyLoadContext. Use it. ", ex: null,
assemblyName);

return null;
}
}

if (_options.AdditionalRuntimePaths?.Any() != true)
{
Log(LogLevel.Warning, "Couldn't locate assembly using {AssemblyName}. Please try adding AdditionalRuntimePaths using " + nameof(PluginLoadContextOptions.Defaults.AdditionalRuntimePaths), ex: null, assemblyName);
Expand All @@ -122,7 +133,7 @@ protected override Assembly Load(AssemblyName assemblyName)

return null;
}

private bool TryUseHostApplicationAssembly(AssemblyName assemblyName)
{
Log(LogLevel.Debug, "Determining if {AssemblyName} should be loaded from host application's or from plugin's AssemblyLoadContext",
Expand All @@ -143,16 +154,43 @@ private bool TryUseHostApplicationAssembly(AssemblyName assemblyName)
return true;
}

var name = assemblyName.Name;
if (_options.UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum.Selected)
{
var name = assemblyName.Name;

var result = _options.HostApplicationAssemblies?.Any(x => string.Equals(x.Name, name, StringComparison.InvariantCultureIgnoreCase)) == true;
var result = _options.HostApplicationAssemblies?.Any(x => string.Equals(x.Name, name, StringComparison.InvariantCultureIgnoreCase)) == true;

Log(LogLevel.Debug, "UseHostApplicationAssemblies is set to Selected. {AssemblyName} listed in the HostApplicationAssemblies: {Result}", ex: null,
assemblyName, result);
Log(LogLevel.Debug, "UseHostApplicationAssemblies is set to Selected. {AssemblyName} listed in the HostApplicationAssemblies: {Result}", ex: null,
assemblyName, result);

return result;
return result;
}

if (_options.UseHostApplicationAssemblies == UseHostApplicationAssembliesEnum.PreferPlugin)
{
Log(LogLevel.Debug, "UseHostApplicationAssemblies is set to PreferPlugin. Try to load assembly from plugin's AssemblyLoadContext and fallback to host application's AssemblyLoadContext",
args: assemblyName);

return false;
}

return false;
}

private bool LoadHostApplicationAssembly(AssemblyName assemblyName)
{
try
{
Default.LoadFromAssemblyName(assemblyName);

return true;
}
catch
{
return false;
}
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
var nativeHint = _runtimeAssemblyHints.FirstOrDefault(x => x.IsNative && string.Equals(x.FileName, unmanagedDllName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public enum UseHostApplicationAssembliesEnum
/// <summary>
/// Always try to use host application's assemblies
/// </summary>
Always
Always,

/// <summary>
/// Prefer plugin's referenced assemblies, fallback to host application's assemblies
/// </summary>
PreferPlugin
}
}
8 changes: 4 additions & 4 deletions tests/Assemblies/JsonNetOld/JsonNetOld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TestIntefaces\TestIntefaces.csproj" />
<ProjectReference Include="..\TestIntefaces\TestIntefaces.csproj"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task CanUseHostsDependencies()
}

[Fact]
public async Task CanUseSelectedHoststDependencies()
public async Task CanUseSelectedHostsDependencies()
{
// Make sure that the referenced version of JSON.NET is loaded into memory
var json = Newtonsoft.Json.JsonConvert.SerializeObject(1);
Expand Down

0 comments on commit 1334210

Please sign in to comment.