tag:help.appveyor.com,2012-11-13:/discussions/problems/15311-building-in-powershell-using-sta-instead-of-default-mtaAppVeyor: Discussion 2018-10-19T08:18:51Ztag:help.appveyor.com,2012-11-13:Comment/457755352018-07-26T02:07:56Z2018-07-26T02:07:58ZBuilding in PowerShell using STA (instead of default MTA)<div><p>I'm writing a WPF framework in PowerShell, so the unit tests involve creating WPF objects.</p>
<p>when I try, I get this error:<br>
Describing Window functionality [-] Returns an object for a textbox 284ms InvalidOperationException: The calling thread must be STA, because many UI components require this. MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "The calling thread must be STA, because many UI components require this." at New-WPFControl, C:\projects\wpfbot3000\WPFBot3000\Private\New-WPFControl.ps1: line 39 at Window, C:\projects\wpfbot3000\WPFBot3000\Public\Window.ps1: line 53 at , C:\projects\wpfbot3000\Tests\Window.Tests.ps1: line 3</p>
<p>I'm using a standard build.ps1/psake/pester strategy, so if there's a way to avoid running pester in a new PowerShell session (passing -STA) I'd prefer it.</p></div>Mike Shepardtag:help.appveyor.com,2012-11-13:Comment/457755352018-07-26T02:32:48Z2018-07-26T02:32:49ZBuilding in PowerShell using STA (instead of default MTA)<div><p>I'm tagging the tests that fail in the CI build with "Local" and excluding them up here. Not ideal, but works for now.</p></div>Mike Shepardtag:help.appveyor.com,2012-11-13:Comment/457755352018-07-26T05:43:52Z2018-07-26T05:43:52ZBuilding in PowerShell using STA (instead of default MTA)<div><p>Hi Mike,</p>
<p>Does the test result differ when you run the same script on your local machine?</p>
<p>The accepted answer to <a href="https://stackoverflow.com/questions/16072041/is-powershell-sta-apartment-state-preferred">this SO post</a> mentions a second way to change the apartment state of a powershell session. Might be worth a try.</p></div>Owen McDonnelltag:help.appveyor.com,2012-11-13:Comment/457755352018-07-26T06:34:12Z2018-07-26T06:34:12ZBuilding in PowerShell using STA (instead of default MTA)<div><p>Yes, it does. The tests all pass when running locally. -STA is the<br>
default for PowerShell, so I'm surprised to get this specific result.</p>
<p>As far as the stackoverflow suggestion, a runspace is essentially a<br>
powershell session, so kicking off the script in a runspace is the<br>
same(-sh) as starting a new PowerShell.exe. A bit nicer to deal with, so<br>
it might be the direction I end up going.</p>
<p>Just added output to the build to show the apartment state to make sure the<br>
error message isn't misleading.</p></div>Mike Shepardtag:help.appveyor.com,2012-11-13:Comment/457755352018-07-26T06:40:49Z2018-07-26T06:40:50ZBuilding in PowerShell using STA (instead of default MTA)<div><p>The build shows MTA, so the error message is not misleading.</p></div>Mike Shepardtag:help.appveyor.com,2012-11-13:Comment/457755352018-08-04T20:01:31Z2018-08-04T20:01:34ZBuilding in PowerShell using STA (instead of default MTA)<div><p>Ended up kicking off a separate powershell session with PowerShell.exe -STA to run the tests and it works fine.</p></div>Mike Shepardtag:help.appveyor.com,2012-11-13:Comment/457755352018-08-05T02:33:27Z2018-08-05T02:33:27ZBuilding in PowerShell using STA (instead of default MTA)<div><p>Sorry Mike, I lost track of this issue until you replied.</p>
<p>Yeah, the only insight i can give is an extremely vague one - that powershell on AppVeyor build machine doesn't run the same as powershell on your local machine.<br>
So I think your workaround is probably best practice in this case.</p></div>Owen McDonnelltag:help.appveyor.com,2012-11-13:Comment/457755352018-08-05T04:55:32Z2018-08-05T04:55:33ZBuilding in PowerShell using STA (instead of default MTA)<div><p>No worries. Just glad I have a solution.</p></div>Mike Shepard