88using System . Collections . ObjectModel ;
99using System . Globalization ;
1010using System . IO ;
11+ using System . Runtime . InteropServices ;
1112using System . Linq ;
1213using System . Management . Automation . Host ;
1314using System . Management . Automation . Remoting ;
1415using System . Management . Automation . Runspaces ;
16+ using System . Reflection ;
1517using System . Text ;
1618using System . Text . RegularExpressions ;
1719using System . Threading ;
@@ -32,6 +34,21 @@ namespace Microsoft.PowerShell.EditorServices
3234 /// </summary>
3335 public class PowerShellContext : IDisposable , IHostSupportsInteractiveSession
3436 {
37+ private const string DotNetFrameworkDescription = ".NET Framework" ;
38+
39+ private static readonly Action < Runspace , ApartmentState > s_runspaceApartmentStateSetter ;
40+
41+ static PowerShellContext ( )
42+ {
43+ // PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection
44+ if ( RuntimeInformation . FrameworkDescription . Equals ( DotNetFrameworkDescription ) )
45+ {
46+ MethodInfo setterInfo = typeof ( Runspace ) . GetProperty ( "ApartmentState" ) . GetSetMethod ( ) ;
47+ Delegate setter = Delegate . CreateDelegate ( typeof ( Action < Runspace , ApartmentState > ) , firstArgument : null , method : setterInfo ) ;
48+ s_runspaceApartmentStateSetter = ( Action < Runspace , ApartmentState > ) setter ;
49+ }
50+ }
51+
3552 #region Fields
3653
3754 private readonly SemaphoreSlim resumeRequestHandle = AsyncUtils . CreateSimpleLockingSemaphore ( ) ;
@@ -175,6 +192,14 @@ public static Runspace CreateRunspace(PSHost psHost)
175192 }
176193
177194 Runspace runspace = RunspaceFactory . CreateRunspace ( psHost , initialSessionState ) ;
195+
196+ // Windows PowerShell must be hosted in STA mode
197+ // This must be set on the runspace *before* it is opened
198+ if ( RuntimeInformation . FrameworkDescription . Equals ( DotNetFrameworkDescription ) )
199+ {
200+ s_runspaceApartmentStateSetter ( runspace , ApartmentState . STA ) ;
201+ }
202+
178203 runspace . ThreadOptions = PSThreadOptions . ReuseThread ;
179204 runspace . Open ( ) ;
180205
0 commit comments