vstest icon indicating copy to clipboard operation
vstest copied to clipboard

Set default ApartmentState to STA on .NET Core 3.0

Open dotMorten opened this issue 7 years ago • 5 comments

Description

With WinForms and WPF being added to .NET Core 3.0 we have a need for being able to run in STA on netcoreapp3.0 targets. I believe this is done by adding the extra target, and changing these lines of codes to also be STA for netcore3.0+

https://github.com/Microsoft/vstest/blob/7b6248203164f8ea821f6795632bd22e0b69afb0/src/Microsoft.TestPlatform.ObjectModel/Constants.cs#L179-L185

I've tried setting it with a test settings file, but it does not appear to have any effect on .NET Core:

<RunSettings>
  <RunConfiguration>
    <!-- STA | MTA  default is STA for .NET Full and MTA for .NET Core-->
    <ExecutionThreadApartmentState>STA</ExecutionThreadApartmentState>
  </RunConfiguration>
</RunSettings>

...Same setttings file has an effect on .NET Framework when setting it to MTA, where it'll behave and fail the same way as .NET Core, so the value does work in one place. This line of code also seem to indicate that it isn't supported currently: https://github.com/Microsoft/vstest/blob/7b6248203164f8ea821f6795632bd22e0b69afb0/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformThread.cs#L15-L18

Steps to reproduce

Running unit tests that require STA thread like the tests in https://github.com/Microsoft/XamlBehaviorsWpf will fail on .NET Core 3.0 with a STA error, whereas it works on net45+ I have a branch here that adds that support. .NET Core tests will fail: https://github.com/dotMorten/XamlBehaviorsWpf/commit/8e6d588e9d3b6a49924b972d9c50d5fe7a97d3dd

Expected behavior

Tests pass as on .NET Framework

Actual behavior

Fails with messages like Message: Test method Microsoft.Xaml.Interactions.UnitTests.ActionTest.AttachDetachTest threw exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The calling thread must be STA, because many UI components require this.

Environment

  • Windows 10 - 1809
  • .NET Core 3.0 Preview 1.

dotMorten avatar Dec 11 '18 08:12 dotMorten

@dotMorten This is a relevant ask with respect to NetCore 3.0. Would you like to contribute?

singhsarab avatar Dec 11 '18 08:12 singhsarab

Is it a breaking behavior to change the default apartment state to STA? If so we'd need to enable it with the configuration (which doesn't currently work). Or some other way?

dotMorten avatar Dec 11 '18 14:12 dotMorten

It will be breaking, I assume for now, so let's keep that as MTA. We should enable the flow via runsettings for now. That should suffice right ?

singhsarab avatar Dec 12 '18 09:12 singhsarab

@dotMorten Would you like to contribute to enable it with the configuration ?

singhsarab avatar Jan 23 '19 13:01 singhsarab

@singhsarab Sorry I don't know where I'd have to start to do this. Also it might mean adding netcore3.0 targets across the board, which is probably a rather large task, and I don't know your project architecture well enough to do that.

dotMorten avatar Feb 06 '19 06:02 dotMorten