SSIS脚本任务抑制onerror

时间:2019-02-13 21:13:10

标签: vb.net http ssis etl script-task

我有一个脚本任务,该任务使用HTTP连接对象下载文件。此脚本任务是一个程序包的一部分,该程序包被另一个程序包调用。有时无法建立连接。在这些情况下,如果连接尝试失败,我将尝试重试几次连接,然后最终引发错误。

我试图实现这一点。它似乎可以正常工作,并且任务没有失败。但是,即使脚本任务没有失败,每次脚本任务中发生异常时,仍然会传播OnError事件。一旦控制权从子包传递回父包,就会失败。

Public Sub Main()
Dim tryTimes As Integer = 0 
Dim maxTimes As Integer = 4 
While (True) 
    Try
        Dim nativeObject As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
            'Create a new HTTP client connection
        Dim connection As New HttpClientConnection(nativeObject)
        Dim filename As String = Dts.Variables("Working_File").Value
        connection.DownloadFile(filename, True)
        Dts.TaskResult = ScriptResults.Success
        Exit While
    Catch ex As Exception
        If (tryTimes < maxTimes) Then
            tryTimes = tryTimes + 1
            Thread.Sleep(30000) 
        Else 
            MsgBox(ex.Message)
            Dts.TaskResult = ScriptResults.Failure
            Throw
        End If
    End Try
End While
End Sub

我希望得到一个解决方案,除非连接尝试失败一定次数,否则不调用OnError事件。

2 个答案:

答案 0 :(得分:1)

尝试编写相同的代码,在前4次尝试中触发警告,而在第5次尝试中触发错误,我不确定是否会起作用:

Public Sub Main()
    Dim tryTimes As Integer = 0 
    Dim maxTimes As Integer = 4 
    While (True) 
        Try
            Dim nativeObject As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
                'Create a new HTTP client connection
            Dim connection As New HttpClientConnection(nativeObject)
            Dim filename As String = Dts.Variables("Working_File").Value
            connection.DownloadFile(filename, True)
            Dts.TaskResult = ScriptResults.Success
            Exit While
        Catch ex As Exception
            If (tryTimes < maxTimes) Then
                tryTimes = tryTimes + 1
                Dts.Events.FireWarning(0, "Error ignored", _
                        "Retrying in 30 seconds", String.Empty, 0)
                Thread.Sleep(30000) 
            Else 
                Dts.Events.FireError(-1, "", "Error message: " & ex2.ToString(), "", 0)
                Dts.TaskResult = ScriptResults.Failure

            End If
        End Try
    End While
End Sub

参考

答案 1 :(得分:1)

您将要在尝试之外使用标签,并在捕获范围内使用GoTo

    Public Sub Main()
        Dim tryTimes As Integer = 0
        Dim maxTimes As Integer = 4

RunCode: 'Label here
        While (True)

            Try
                'your code here

                Exit While
            Catch ex As Exception
                If (tryTimes < maxTimes) Then
                    tryTimes = tryTimes + 1
                    Thread.Sleep(30000)
                    GoTo RunCode 'after we catch the exception and eval tryTimes go back and retry
                Else

                    'MsgBox(ex.Message)
                    Dts.Events.FireError(-1, "", "Error message: " & ex.ToString(), "", 0)
                    Dts.TaskResult = ScriptResults.Failure
                    'Throw
                End If
            End Try
        End While
    End Sub