This is to discuss and potentially document current behaviour (that is inconsistent with Windows PowerShell). Maybe some porting work was forgotten since the APIs were initially not available in .Net Core 1.
When creating a new PowerShell instance using [Management.Automation.PowerShell]::Create() (or when creating a new runspace using [runspacefactory]::CreateRunspace()), then PowerShell Core and Windows PowerShell are both in MTA mode. This seems to be reasonable.
When being in the shell of PowerShell Core, the default ApartmentState is also MTA, which seems OK as well at first.
However, Windows PowerShell is in STA mode by default and powershell.exe even has an MTA and STA switch, which PowerShell Core does not have.
My questions:
- Why was
STA chosen to be the default when being in a shell or ConsoleHost of Windows PowerShell? If there was a reason, we should reconsider it for PowerShell Core. I should add though that this inconsistency could lead to a situation that is difficult to debug (we had a case where execution in the shell was fine but when using our PowerShell runner that uses the PowerShell APIs, we were experiencing threading issues and scratching our heads for some time what the reason could be until we found out about the inconsistency in Windows PowerShell)
- Does
pwsh need an STA command line option? In our case we can workaround it by setting it ourselves by setting it in the InitialSessionState that we then pass to PowerShell.Create()
This is to discuss and potentially document current behaviour (that is inconsistent with Windows PowerShell). Maybe some porting work was forgotten since the APIs were initially not available in .Net Core 1.
When creating a new PowerShell instance using
[Management.Automation.PowerShell]::Create()(or when creating a new runspace using[runspacefactory]::CreateRunspace()), then PowerShell Core and Windows PowerShell are both inMTAmode. This seems to be reasonable.When being in the shell of
PowerShell Core, the default ApartmentState is alsoMTA, which seems OK as well at first.However, Windows PowerShell is in
STAmode by default andpowershell.exeeven has anMTAandSTAswitch, which PowerShell Core does not have.My questions:
STAchosen to be the default when being in a shell or ConsoleHost of Windows PowerShell? If there was a reason, we should reconsider it for PowerShell Core. I should add though that this inconsistency could lead to a situation that is difficult to debug (we had a case where execution in the shell was fine but when using our PowerShell runner that uses the PowerShell APIs, we were experiencing threading issues and scratching our heads for some time what the reason could be until we found out about the inconsistency in Windows PowerShell)pwshneed anSTAcommand line option? In our case we can workaround it by setting it ourselves by setting it in theInitialSessionStatethat we then pass toPowerShell.Create()