瓦廷和 ApartmentState.STA

发布于 2024-12-22 18:01:30 字数 387 浏览 0 评论 0原文

问题:“CurrentThread 需要将其 ApartmentState 设置为 ApartmentState.STA 才能自动化 Internet Explorer。”

首先,我已经阅读了上述问题的所有解决方案,但没有一个对我有用。可能是我遗漏了一些东西。我尝试将执行线程条目添加到我的 app.config 中,还尝试设置 STAThread 属性,但我仍然面临与上述相同的异常。

工具:Visual Studio 2010、Watin 2.1、c#

场景:尝试在单击按钮时从 Web 应用程序运行单元测试 [c# 中的 watin 脚本]。但是当脚本即将在以下行启动 IE 时,会引发上述异常: IE mybrowser = new IE("SomeURL here");

有什么想法吗?

Issue: "The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer."

First of all i have read all the solutions to the problems above and none works for me.May be i am missing something. I have tried adding Execution Thread entry into my app.config, also tried setting STAThread attribute and i am still facing the same exception as stated above.

Tools: Visual Studio 2010, Watin 2.1, c#

Scenario: Trying to run a unit test [watin script in c#] from a web application upon a button click. But the above exception is thrown when the script is about to launch IE on the following line :
IE mybrowser = new IE ("SomeURL here");

Any Thoughts ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青柠芒果 2024-12-29 18:01:30

从朋友那里得到的。我们实际上不必添加任何 app.config 条目。只需在单一状态下启动线程即可。就我而言,我在按钮单击处理程序中编写了以下代码:

System.Threading.Thread th = new Thread(new ThreadStart(Test));
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
        th.Join();

and i moved the call to unit test in the private. TEST method as follows:
 private void Test()
    {
        var som = new Project.ClassName();
        som.MethodToExecute();
}

Got it from a friend. We actually dont have to add any app.config entry. Just start the thread in a single state. In my case, i wrote the following code in my button click handler:

System.Threading.Thread th = new Thread(new ThreadStart(Test));
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
        th.Join();

and i moved the call to unit test in the private. TEST method as follows:
 private void Test()
    {
        var som = new Project.ClassName();
        som.MethodToExecute();
}
飘逸的'云 2024-12-29 18:01:30

您的 App.Config 是什么样的?

  <NUnit>
    <TestRunner>
      <!-- Valid values are STA,MTA. Others ignored. -->
      <add key="ApartmentState" value="STA"/>
    </TestRunner>
  </NUnit>

以上适用于我的Win7、IE9(32位)和Watin2.1。它还适用于 WinXP、IE8、WatiN 2.1。我 99% 确信它在以前版本的 WatiN 上也能正常工作。不需要进行其他 ApartmentState 更改。

What does your App.Config look like?

  <NUnit>
    <TestRunner>
      <!-- Valid values are STA,MTA. Others ignored. -->
      <add key="ApartmentState" value="STA"/>
    </TestRunner>
  </NUnit>

The above works for me on Win7, IE9 (32bit), and Watin2.1. It also works on WinXP, IE8, WatiN 2.1. I'm 99% sure it worked just fine on previous versions of WatiN as well. No other ApartmentState changes were needed.

意犹 2024-12-29 18:01:30

只需在文件顶部或项目的入口点添加 [ assembly: RequiresSTA] 即可。

Simply add [assembly: RequiresSTA] at the top of your file or at the entry point of your project.

辞慾 2024-12-29 18:01:30

我已经完成了 app.config 更改,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="NUnit">
      <section name="TestRunner" type="System.Configuration.NameValueSectionHandler" />
    </sectionGroup>
  </configSections>
  <NUnit>
    <TestRunner>
      <add key="ApartmentState" value="STA" />
    </TestRunner>
  </NUnit>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

但没有骰子。我打开 AssemblyInfo 并添加

[assembly: RequiresSTA]

,突然间,宇宙开始再次正常运行。

I had already done the app.config changes

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="NUnit">
      <section name="TestRunner" type="System.Configuration.NameValueSectionHandler" />
    </sectionGroup>
  </configSections>
  <NUnit>
    <TestRunner>
      <add key="ApartmentState" value="STA" />
    </TestRunner>
  </NUnit>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

but no dice. I opened AssemblyInfo and added

[assembly: RequiresSTA]

and suddenly, the universe began to function properly again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文