Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default ApartmentState to STA on .NET Core 3.0 #1865

Open
dotMorten opened this issue Dec 11, 2018 · 5 comments
Open

Set default ApartmentState to STA on .NET Core 3.0 #1865

dotMorten opened this issue Dec 11, 2018 · 5 comments
Labels

Comments

@dotMorten
Copy link
Contributor

@dotMorten dotMorten commented Dec 11, 2018

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.
@singhsarab

This comment has been minimized.

Copy link
Member

@singhsarab singhsarab commented Dec 11, 2018

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

@dotMorten

This comment has been minimized.

Copy link
Contributor Author

@dotMorten dotMorten commented Dec 11, 2018

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?

@singhsarab

This comment has been minimized.

Copy link
Member

@singhsarab singhsarab commented Dec 12, 2018

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 ?

@abhishkk abhishkk added the bug label Dec 18, 2018
@singhsarab

This comment has been minimized.

Copy link
Member

@singhsarab singhsarab commented Jan 23, 2019

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

@dotMorten

This comment has been minimized.

Copy link
Contributor Author

@dotMorten dotMorten commented Feb 6, 2019

@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.

@cltshivash cltshivash removed the bug label Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.