为什么我得到一个无效操作异常(非STA线程?)上运行的TeamCity这MSpec测试? [英] Why do I get an Invalid Operation Exception (non STA thread?) running this MSpec test on TeamCity?

查看:870
本文介绍了为什么我得到一个无效操作异常(非STA线程?)上运行的TeamCity这MSpec测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于我的应用程序到.NET 4的迁移过程中,我努力获得一些WPF单元测试与TeamCity的再次合作。

在所有以某种方式使用WPF控件(一个列表项为例)的测试中,我得到一个例外,我没有得到之前:

  System.InvalidOperationException:调用线程必须为STA,因为许多UI组件都需要这一点。
 

我明白这意味着什么,并检查后,事实证明,我的线程的确是MTA,而不是STA。

我的问题是,我不知道如何解决这个问题,并在此问题可以从哪里来? 它是在TeamCity的设置? MSpec? 再次,它的工作之前,我切换到.NET 4中。

我尝试了许多不同的解决方案,但毫无效果。

我也受了一点事实,没有人举报此之前疑惑(我具体的TeamCity + MSpec + WPF试验堆),这可能意味着我做的事情非常错误的地方。

如果您有线索,请让我知道!

完全不同:

  System.InvalidOperationException:调用线程必须为STA,因为许多UI组件都需要这一点。


 在System.Windows.Input.InputManager..ctor()
   在System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
   在System.Windows.Input.KeyboardNavigation..ctor()
   在System.Windows.FrameworkElement.EnsureFrameworkServices()
   在System.Windows.FrameworkElement..ctor()
   在System.Windows.Controls.Control..ctor()
   在MyCompany.Dashboard.Client.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.ConfigureViewModel.CreateItem(String名)在d:\ PROGRAM Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\$c$c\Src\MyCompany\Dashboard\Client\Plugins\Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModel.cs:line 171
   在MyCompany.Dashboard.Client.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.ConfigureViewModel.Initialise(Type类型,IList`1 currentSelection,Action`1 selectionChangedCallback)在d:\ PROGRAM Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\$c$c\Src\MyCompany\Dashboard\Client\Plugins\Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModel.cs:line 37
   在UnitTests.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.when_some_items_are_selected_on_the_chosen_list.<.ctor>b__1()在d:\ PROGRAM Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\$c$c\Src\UnitTests.Plugins.Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModelTests.cs:line 82
 

有关此异常,code只是想实例化一个ListBoxItem的,没有什么花哨,但这样做在一个MTA线程打破它。

我的尝试:

  • 设置当前线程STA

    Thread.CurrentThread.SetApartmentState(ApartmentState.STA)

这当然不行,因为它是唯一可能的线程开始之前

  • 运行code在被初始化为STA一个单独的线程: 很复杂,因为,由于MSpec的性质,不同的方法被称为在不同的时间,所以你不能运行在同一个线程下的一切。更多precisely,你不能运行在同一个线程中建立环境的声明因。

  • 使用STAThread属性...是的,但在哪里?从来没有在任何地方我 尝试

一个失败的测试的例子:

 公共类StaTestExample
{
    建立上下文=()=&GT; _control =新的ListBox();

    它should_not_be_null =()=&GT; _control.ShouldNotBeNull();

    受保护的静态控制_control;
}
 

解决方案

现在的工作。

但问题是,我们只是不能解释它。它仍然失败不同的生成服务器上,但我们不关心这个。

如果有人得到这个问题,在这里我们做了什么:

  • 禁用测试覆盖
  • 禁用MSPec任务:构建云绿
  • 重新启用覆盖和MSpec:它的工作原理...

奇怪的是,确切的过程被应用于不同的构建服务器(一个旧的,我们不使用了)上,它仍然失败。

还有没有别的,我们可以想到的改变。

所以这是一个有点神秘...我希望它不会回来咬我们!

As part of the migration of my app to .NET 4, I'm struggling to get some of the WPF unit tests working again with TeamCity.

On all the tests that are somehow using a WPF control (a ListItem for example), I get an exception I didn't get before:

System.InvalidOperationException: The calling thread must be STA, because many UI components require this.

I understand what it means, and after checking, it turns out that my thread is indeed MTA, not STA.

My problem is that I have no idea on how to fix this, and where this problem could be coming from... Is it a setting on TeamCity? MSpec? Again, it worked before I switched to .NET 4.

I tried many different solutions, but nothing worked.

I'm also a bit puzzled by the fact that no-one reported this before (with my specific stack of TeamCity + MSpec + WPF test), which might mean I'm doing something very wrong somewhere.

If you have a clue, please let me know!

Full exception:

System.InvalidOperationException: The calling thread must be STA, because many UI components require this.


 at System.Windows.Input.InputManager..ctor()
   at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
   at System.Windows.Input.KeyboardNavigation..ctor()
   at System.Windows.FrameworkElement.EnsureFrameworkServices()
   at System.Windows.FrameworkElement..ctor()
   at System.Windows.Controls.Control..ctor()
   at MyCompany.Dashboard.Client.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.ConfigureViewModel.CreateItem(String name) in d:\Program Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\Code\Src\MyCompany\Dashboard\Client\Plugins\Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModel.cs:line 171
   at MyCompany.Dashboard.Client.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.ConfigureViewModel.Initialise(Type type, IList`1 currentSelection, Action`1 selectionChangedCallback) in d:\Program Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\Code\Src\MyCompany\Dashboard\Client\Plugins\Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModel.cs:line 37
   at UnitTests.Plugins.Common.Controls.Grids.CashflowGrid.ViewModel.when_some_items_are_selected_on_the_chosen_list.<.ctor>b__1() in d:\Program Files\JetBrains\BuildAgent2\work\6dd9af6ae2f9bbb9\Code\Src\UnitTests.Plugins.Common\Controls\Grids\CashflowGrid\ViewModel\ConfigureViewModelTests.cs:line 82

For this exception, the code is simply trying to instantiate a ListBoxItem, nothing fancy, but doing that on a MTA thread breaks it.

What I tried:

  • Setting the current thread to STA

    Thread.CurrentThread.SetApartmentState(ApartmentState.STA)

It of course doesn't work because it's only possible before the thread starts

  • Run the code in a separate thread that was initialised as STA: very complex since, due to the nature of MSpec, different methods are called at different time so you can't run EVERYTHING under the same thread. More precisely, you can't run the "Establish context" on the same thread as the "Because of" statement.

  • Use the STAThread attribute... yes but where? never worked anywhere I tried

Example of a failing test:

public class StaTestExample
{
    Establish context = () => _control = new ListBox();

    It should_not_be_null = () => _control.ShouldNotBeNull();

    protected static Control _control;
}

解决方案

It now works.

But the problem is we just can't explain it. And it still fails on a different build server, but we don't care about this one.

In case someone gets that problem, here what we did:

  • Disabling test coverage
  • Disabling the MSPec task: the build goes green
  • Re-enabling coverage and MSpec: it works...

The odd thing is that exact process was applied on a different build server (an old one we don't use anymore), and it still fails.

There's nothing else we could think of that changed.

So it's a bit of a mystery... I hope it won't come back to bite us!

这篇关于为什么我得到一个无效操作异常(非STA线程?)上运行的TeamCity这MSpec测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆