本文介绍了华廷:本CurrentThread需要具有它的ApartmentState设为ApartmentState.STA能够自动化的Internet Explorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个C#窗口服务调用华廷。当我调用它华廷抛出以下异常。的CurrentThread需要具有它的ApartmentState设为ApartmentState.STA能够自动化的Internet Explorer

I am calling WatiN from a C# windows service. When I invoke WatiN it throws the following exception. The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer

我试图启动一个线程并经由设置单元状态

I have tried starting up a thread and setting the apartment state via

mythread.SetApartmentState(ApartmentState.STA)

但导致另一个错误

*无法因为代码优化或本机框架之上,以评估表达调用堆栈。*

我也尝试添加到服务入口点的属性。

I also tried adding an attribute to the Service entry point.

static class Program
{
      [STAThread]
      static void Main()
      {
          ...

任何想法?

推荐答案

我知道本杰明已经发布了工作的答案,但我想我会加入一两件事情我已经经历时,我已经得到了试图执行华廷测试时此错误:
对于NUnit的,你应该添加这样的事情你的app.config为测试:

I know Benjamin's already posted a 'working' answer, but I thought I'd add a couple of things I've experienced when I've got this error when trying to execute WatiN tests:For NUnit, you should add something like this to your app.config for the tests:

  <configSections>
    <sectionGroup name="NUnit">
      <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>
  <NUnit>
    <TestRunner>
      <!-- WatiN can only host IE in STA mode -->
      <add key="ApartmentState" value="STA"/>
    </TestRunner>
  </NUnit>

在MbUnit的,修改的TestFixture属性是这样的:

In MbUnit, modify your TestFixture attribute like this:

[TestFixture(ApartmentState = ApartmentState.STA)] 

HTH,
皮特

HTH,Pete.

哈 - 它实际上是在文档中。卫生署!

Ha - it's actually in the documentation. Doh!http://watin.org/documentation/sta-apartmentstate/

这篇关于华廷:本CurrentThread需要具有它的ApartmentState设为ApartmentState.STA能够自动化的Internet Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 00:14