SQL 作业出错,调用目标抛出异常

Error in a SQL Job, Exception has been thrown by the target of an invocation

我在 SSIS 项目中遇到了一个大问题。

当我在 SSIS 本地计算机中执行项目时,dtsx 运行良好,但是当我将其放入 SQL 代理作业时,它会抛出一个错误:

Source: File In Folder? Description: Exception has been thrown by the target of an invocation. End Error Error: 2020-10-02 17:37:05.33 Code: 0x00000001 Source: Script Task 1 Description: Exception has been thrown by the target of an invocation. End Error DTExec: The package execution returned DTSER_FAILURE (1). Started: 17:37:01 Finished: 17:37:05 Elapsed: 3.687 seconds. The package execution failed. The step failed.

文件夹中的源文件是脚本任务,代码如下:

    Dim StrFolderArrary As String()
    Dim StrFileArray As String()
    Dim fileName As String
    Dim RemoteDirectory As String

    RemoteDirectory = Dts.Variables("User::ftp_masks").Value.ToString()

    Dim cm As ConnectionManager = Dts.Connections("FTP Connection Manager") 'FTP connection manager name
    Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing))

    Try
        ftp.Connect() 'Connecting to FTP Server

        ftp.SetWorkingDirectory(RemoteDirectory) 'Provide the Directory on which you are working on FTP Server

        ftp.GetListing(StrFolderArrary, StrFileArray) 'Get all the files and Folders List

        'If there is no file in the folder, strFile Arry will contain nothing, so close the connection.

        If StrFileArray Is Nothing Then

            MessageBox.Show(Dts.Variables("User::FileExistsFlg").Value.ToString())

            Dts.Variables("User::FileExistsFlg").Value = 0

            ftp.Close()


            'If Files are there, Loop through the StrFileArray arrary and insert into table
        Else

            For Each fileName In StrFileArray

                MessageBox.Show(fileName)
                Dts.Variables("User::files").Value = fileName
                MessageBox.Show(Dts.Variables("User::files").Value.ToString())
                If fileName = Dts.Variables("User::files").Value.ToString() Then
                    Dts.Variables("User::FileExistsFlg").Value = 1
                    MessageBox.Show(Dts.Variables("User::FileExistsFlg").Value.ToString())
                End If
            Next
            Dts.TaskResult = ScriptResults.Success

            ftp.Close()

        End If

        MessageBox.Show("End try")

    Catch ex As Exception

        MessageBox.Show("Catch")
        Dts.Events.FireError(0, "My File Task", ex.Message, String.Empty, 0)
        MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
        Throw New ApplicationException("Something happened :(", ex)
        'Dts.TaskResult = ScriptResults.Failure
    Finally
        ' This line executes whether or not the exception occurs.
        MessageBox.Show("in Finally block")

    End Try

    '
    ' Add your code here
    '
    Dts.TaskResult = ScriptResults.Success

有人可以帮助我吗? 如何查看具体错误?

感谢您的宝贵时间。

您的代码面临的挑战之一是 MessageBox 的使用。那是一个交互式 UI 元素。当 SSIS 包以无人值守模式运行时,您不能使用它们,因为服务器上会弹出一个窗口,阻止所有进度,直到有人登录并单击“确定”。是的,这完全发生在 DTS 时代(2005 年之前)。

保留弹出窗口的一种方法是测试 InteractiveMode 的系统范围变量。您需要在 ReadOnly 变量列表中检查它,然后将您的代码修改为

VB近似

' This is not valid VB syntax but I can't remember their declaration/initialization sytnax
Dim interactiveMode as Boolean =  (bool) Dts.Variables["System::InteractiveMode"].Value

if interactiveMode then
    MessageBox.Show(fileName)
End If

比这更好的方法是 raise Information messages。这些工作都是无人值守的。

这是 C#,欢迎您转换为 VB 语法

bool fireAgain = false;
string message = "{0}:{1}";

Dts.Events.FireInformation(0, "SCR Echo Back", string.Format(message, "RemoteDirectory", RemoteDirectory), string.Empty, 0, ref fireAgain);

Dts.Events.FireInformation(0, "SCR Echo Back", string.Format(message, "fileName", fileName), string.Empty, 0, ref fireAgain);

这会将远程目录和文件名值转储到信息日志中。在 VS 中,这将显示在输出 window 以及包执行结果图形 window 中。在服务器上,这将转储到 SSISDB.catalog.OperationMessages

说了这么多,还能发生什么?

  • 我好像记得ftp如果文件夹为空,客户端会抛出异常。
  • 服务器可能有一个 firewall/anti-virus 阻止对端口 22/23 的出站访问 (ftp).
  • 您的连接管理器实例化可能会引发异常
  • 与 ftp 客户端连接相同。
  • 如果这不是匿名访问,则可能是服务器版本的代码未获取凭据信息,因为它在部署时已从基础包中删除