问题:" CurrentThread需要将它的ApartmentState设置为ApartmentState。STA能够使Internet Explorer自动化。"
首先,我已经阅读了以上所有问题的解决方案,但没有一个适合我。也许我错过了什么。我试过在我的app.config中添加执行线程条目,也试过设置STAThread属性,我仍然面临如上所述的相同异常。
工具:Visual Studio 2010, Watin 2.1, c#场景:尝试从web应用程序运行单元测试[c#脚本]在一个按钮点击。但是,当脚本即将在以下行启动IE时,会抛出上述异常:IE mybrowser = new IE ("SomeURL here");
有什么想法吗?
朋友送的。我们实际上不需要添加任何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();
}
你的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, windows2.1。我有99%的把握它在以前版本的WatiN上也能正常工作。不需要更改其他aparmentstate。
只需在文件的顶部或项目的入口点添加[assembly: RequiresSTA]
我已经修改了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]
突然间,宇宙又开始正常运转了