diff --git a/test/csharp/fixture_AssemblyLoadContext.cs b/test/csharp/fixture_AssemblyLoadContext.cs new file mode 100644 index 0000000000..71362729e3 --- /dev/null +++ b/test/csharp/fixture_AssemblyLoadContext.cs @@ -0,0 +1,26 @@ +using Xunit; +using System; +using System.Management.Automation; +using Microsoft.PowerShell.Linux.Host; + +// This collection fixture initializes Core PowerShell's AssemblyLoadContext once and only +// once. Attempting to initialize in a class level fixture will cause multiple +// initializations, resulting in test failure due to "Binding model is already locked for +// the AppDomain and cannot be reset". +namespace PSTests +{ + public class AssemblyLoadContextFixture + { + public AssemblyLoadContextFixture() + { + // Initialize the Core PowerShell AssemblyLoadContext + PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(AppContext.BaseDirectory); + } + } + + [CollectionDefinition("AssemblyLoadContext")] + public class AssemblyLoadContextCollection : ICollectionFixture + { + // nothing to do but satisfy the interface + } +} diff --git a/test/csharp/test_Runspace.cs b/test/csharp/test_Runspace.cs index 96dbe6355f..0a7b00eb98 100644 --- a/test/csharp/test_Runspace.cs +++ b/test/csharp/test_Runspace.cs @@ -9,17 +9,12 @@ namespace PSTests // NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults, // otherwise Invoke will not return any objects + [Collection("AssemblyLoadContext")] public class RunspaceTests { private static int count = 3; private static string script = String.Format($"get-process | select-object -first {count}"); - // Initialize the Core PowerShell AssemblyLoadContext - public RunspaceTests() - { - PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(AppContext.BaseDirectory); - } - [Fact] public void TestRunspaceWithPipeline() {