SSIS 脚本组件上的 WATIN - 单线程单元

发布于 2024-10-28 11:32:40 字数 694 浏览 0 评论 0原文

您好,我需要从具有表单身份验证的站点下载文件,并使用集成服务进一步处理文本文件。

对于文件下载,我选择使用 WATIN,因此我导入了 Watin 库并编写了浏览器步骤的脚本。但是,当我尝试运行代码时,我收到一条带有此消息的异常。

CurrentThread 需要有它的 ApartmentState 设置为 ApartmentState.STA能够 自动化 Internet Explorer。

所有这一切都使用 (使用方法属性)

如果我尝试使用这行代码将其设置为 STA

System.Threading.Thread.CurrentThread.SetApartmentState(Threading.ApartmentState.STA)

我会收到此异常

错误: System.Reflection.TargetInitationException: 异常已被抛出 调用的目标。 ---> 系统.InvalidOperationException: 无法设置指定的COM 公寓状态。在 System.Threading.Thread.SetApartmentState(公寓状态 状态)

如何更改 SSIS 脚本任务以使用此单线程单元?

Hi I need to download a file from a site with forms authentication and further processing the text file with Integration Services.

For the file download I choose to use WATIN, So I imported the Watin library and scripted the browser steps. However when I try to run the code I get an Exception with this Message.

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

all this using the
(with the method attribute)

If I try to use this line of code to set it to STA

System.Threading.Thread.CurrentThread.SetApartmentState(Threading.ApartmentState.STA)

I get this exception

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)

How do I change the SSIS script task to use this Single-Threaded apartment?

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

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

发布评论

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

评论(2

过去的过去 2024-11-04 11:32:40

我通过使用 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

希望这可以帮助遇到同样问题的人。

I've solved this by creating another Thread with the STA ApartmentState.

The code was like this:

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

Hope this helps someone with the same problem.

吃素的狼 2024-11-04 11:32:40

我发现您的解决方案对于我的类似情况非常有用,我添加了我使用的 C# 代码,以防有人需要它

public void Main() {
    Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join(); // wait for thread to end

    Dts.TaskResult = (int)ScriptResults.Success;
}

public void DoMethod(object sender) {
    System.Windows.Forms.Application.Run(new BrowserWindow("http://x.xxx"));
}

,这是 BrowserWindow

class BrowserWindow : Form
{
    private string url;

    public BrowserWindow(string url) {
        this.url = url;
        ShowInTaskbar = false;
        WindowState = FormWindowState.Minimized;
        Load += new EventHandler(Window_Load);
    }

    void Window_Load(object sender, EventArgs e) {
        WebBrowser wb = new WebBrowser();
        wb.AllowNavigation = true;
        wb.DocumentCompleted += wb_DocumentCompleted;
        wb.Navigate(this.url);
    }

    void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser web = sender as WebBrowser;
        // here goes the business logic
    }
}

I found your solution very useful for my case that is similar, I add the C# code I used in case someone needs it

public void Main() {
    Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join(); // wait for thread to end

    Dts.TaskResult = (int)ScriptResults.Success;
}

public void DoMethod(object sender) {
    System.Windows.Forms.Application.Run(new BrowserWindow("http://x.xxx"));
}

And here is the BrowserWindow class

class BrowserWindow : Form
{
    private string url;

    public BrowserWindow(string url) {
        this.url = url;
        ShowInTaskbar = false;
        WindowState = FormWindowState.Minimized;
        Load += new EventHandler(Window_Load);
    }

    void Window_Load(object sender, EventArgs e) {
        WebBrowser wb = new WebBrowser();
        wb.AllowNavigation = true;
        wb.DocumentCompleted += wb_DocumentCompleted;
        wb.Navigate(this.url);
    }

    void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser web = sender as WebBrowser;
        // here goes the business logic
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文