-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Test for Loading Plugins in non-default ALC.
- Loading branch information
Showing
14 changed files
with
918 additions
and
625 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using WithOurPluginsPluginContract; | ||
|
||
namespace WithOurPluginsPluginA | ||
{ | ||
public class Class1 : ISayHello | ||
{ | ||
public string SayHello() => $"Hello from {nameof(WithOurPluginsPluginA)}"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
test/TestProjects/WithOurPluginsPluginA/WithOurPluginsPluginA.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WithOurPluginsPluginContract\WithOurPluginsPluginContract.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using WithOurPluginsPluginContract; | ||
|
||
namespace WithOurPluginsPluginB | ||
{ | ||
public class Class1 : ISayHello | ||
{ | ||
public string SayHello() => $"Hello from {nameof(WithOurPluginsPluginB)}"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
test/TestProjects/WithOurPluginsPluginB/WithOurPluginsPluginB.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WithOurPluginsPluginContract\WithOurPluginsPluginContract.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace WithOurPluginsPluginContract | ||
{ | ||
public interface ISayHello | ||
{ | ||
string SayHello(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/TestProjects/WithOurPluginsPluginContract/WithOurPluginsPluginContract.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using McMaster.NETCore.Plugins; | ||
using WithOurPluginsPluginContract; | ||
using WithOwnPluginsContract; | ||
|
||
namespace WithOwnPlugins | ||
{ | ||
public class WithOwnPlugins : IWithOwnPlugins | ||
{ | ||
public bool TryLoadPluginsInCustomContext(AssemblyLoadContext? callingContext) | ||
{ | ||
var currentContext = AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()); | ||
if (currentContext == callingContext) | ||
{ | ||
throw new ArgumentException("The context of the caller is the context of this assembly. This invalidates the test."); | ||
} | ||
|
||
#if NETCOREAPP3_0 | ||
/* | ||
Ensure the source calling context does not have our plugin's interfaces loaded. | ||
This guarantees that the Assembly cannot possibly unify with the default load context. | ||
Note: | ||
The code below this check would fail anyway if the assembly does unify with the default context. | ||
This is more of a safety check to ensure "correctness" as opposed to anything else. | ||
*/ | ||
var sayHelloAssembly = typeof(ISayHello).Assembly; | ||
if (callingContext?.Assemblies.Contains(sayHelloAssembly) == true) // .Assemblies API not available in Core 2.X | ||
{ | ||
throw new ArgumentException("The context of the caller has this plugin's interface to interact with its own plugins loaded. Test is void."); | ||
} | ||
#endif | ||
|
||
// Load our own plugins: Remember, we are in an isolated, non-default ALC. | ||
var plugins = new List<ISayHello?>(); | ||
string[] assemblyNames = { "Plugins/WithOurPluginsPluginA.dll", "Plugins/WithOurPluginsPluginB.dll" }; | ||
|
||
foreach (var assemblyName in assemblyNames) | ||
{ | ||
var currentAssemblyFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Unable to get folder path for currently executing assembly."); | ||
var pluginPath = Path.Combine(currentAssemblyFolderPath, assemblyName); | ||
|
||
using var loader = PluginLoader.CreateFromAssemblyFile(pluginPath, new[] { typeof(ISayHello) }); | ||
var assembly = loader.LoadDefaultAssembly(); | ||
var configType = assembly.GetTypes().First(x => typeof(ISayHello).IsAssignableFrom(x) && !x.IsAbstract); | ||
var plugin = (ISayHello?)Activator.CreateInstance(configType); | ||
if (plugin == null) | ||
{ | ||
throw new Exception($"Failed to load instance of {nameof(ISayHello)} from plugin."); | ||
} | ||
|
||
plugins.Add(plugin); | ||
} | ||
|
||
// Shouldn't need to check for this but just in case to absolutely make sure. | ||
if (plugins.Any(plugin => String.IsNullOrEmpty(plugin?.SayHello()))) | ||
{ | ||
throw new Exception("No value returned from plugin."); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Plugins\McMaster.NETCore.Plugins.csproj" /> | ||
<ProjectReference Include="..\WithOurPluginsPluginA\WithOurPluginsPluginA.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" OutputItemType="Content" CopyToOutputDirectory="Always" Link="Plugins/%(RecursiveDir)%(Filename).dll" /> | ||
<ProjectReference Include="..\WithOurPluginsPluginB\WithOurPluginsPluginB.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" OutputItemType="Content" CopyToOutputDirectory="Always" Link="Plugins/%(RecursiveDir)%(Filename).dll" /> | ||
<ProjectReference Include="..\WithOurPluginsPluginContract\WithOurPluginsPluginContract.csproj" /> | ||
<ProjectReference Include="..\WithOwnPluginsContract\WithOwnPluginsContract.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Runtime.Loader; | ||
|
||
namespace WithOwnPluginsContract | ||
{ | ||
public interface IWithOwnPlugins | ||
{ | ||
bool TryLoadPluginsInCustomContext(AssemblyLoadContext? callingContext); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/TestProjects/WithOwnPluginsContract/WithOwnPluginsContract.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
</Project> |