diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 170cd0d20..7ac777889 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -8,10 +8,12 @@ using System.Collections.ObjectModel; using System.Globalization; using System.IO; +using System.Runtime.InteropServices; using System.Linq; using System.Management.Automation.Host; using System.Management.Automation.Remoting; using System.Management.Automation.Runspaces; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -31,6 +33,21 @@ namespace Microsoft.PowerShell.EditorServices /// public class PowerShellContext : IDisposable, IHostSupportsInteractiveSession { + private const string DotNetFrameworkDescription = ".NET Framework"; + + private static readonly Action s_runspaceApartmentStateSetter; + + static PowerShellContext() + { + // PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection + if (RuntimeInformation.FrameworkDescription.Equals(DotNetFrameworkDescription)) + { + MethodInfo setterInfo = typeof(Runspace).GetProperty("ApartmentState").GetSetMethod(); + Delegate setter = Delegate.CreateDelegate(typeof(Action), firstArgument: null, method: setterInfo); + s_runspaceApartmentStateSetter = (Action)setter; + } + } + #region Fields private readonly SemaphoreSlim resumeRequestHandle = AsyncUtils.CreateSimpleLockingSemaphore(); @@ -174,6 +191,14 @@ public static Runspace CreateRunspace(PSHost psHost) } Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState); + + // Windows PowerShell must be hosted in STA mode + // This must be set on the runspace *before* it is opened + if (RuntimeInformation.FrameworkDescription.Equals(DotNetFrameworkDescription)) + { + s_runspaceApartmentStateSetter(runspace, ApartmentState.STA); + } + runspace.ThreadOptions = PSThreadOptions.ReuseThread; runspace.Open();