您好,我需要从具有表单例份验证的站点下载文件,并使用集成服务进一步处理该文本文件。
对于文件下载,我选择使用 WATIN,因此我导入了 Watin 库并编写了浏览器步骤的脚本。但是,当我尝试运行代码时,我收到一 strip 有此消息的异常。
The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
所有这一切都使用 (使用方法属性)
如果我尝试使用这行代码将其设置为 STA
System.Threading.Thread.CurrentThread.SetApartmentState(Threading.ApartmentState.STA)
我得到这个异常
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Failed to set the specified COM apartment state. at System.Threading.Thread.SetApartmentState(ApartmentState state)
如何更改 SSIS 脚本任务以使用此单线程单元?
最佳答案
我通过使用 STA ApartmentState 创建另一个线程解决了这个问题。
代码是这样的:
Private threadDelegate As ParameterizedThreadStart
Private filesdir As String
<STAThread()> _
Public Sub Main()
Try
threadDelegate = New ParameterizedThreadStart(AddressOf Me.DoSomething)
StartBrowserRoutine(threadDelegate)
Dts.TaskResult = ScriptResults.Success
Catch
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
Private Sub StartBrowserRoutine(ByVal threadRoutine As ParameterizedThreadStart)
Dim dialogThread As Thread = New Thread(threadRoutine)
dialogThread.TrySetApartmentState(ApartmentState.STA)
dialogThread.Start(Nothing)
dialogThread.Join(System.Threading.Timeout.Infinite)
End Sub
Private Sub DoSomething()
'Do Something
End Sub
希望这可以帮助遇到同样问题的人。
关于ssis - SSIS 脚本组件上的 WATIN - 单线程单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503079/