调用目标抛出异常(SSIS & WinSCP & C#)

Exception has been thrown by the target of an invocation (SSIS & WinSCP & C#)

我在 SSIS 中尝试执行脚本任务时收到此错误消息(我使用的是 VS 2019 和 C#)。

试了一段时间好像不行。

Error message: Exception has been thrown by the target of an invocation

详情:

   in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   in System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   in System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   in Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

这是我的代码:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Linq;
using WinSCP;
#endregion

namespace ST_2cfb1a0997cf7b332da4453v3g1132g
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
            public void Main()
            {

                    SessionOptions sessionOptions = new SessionOptions
                    {
                        Protocol = Protocol.Sftp,
                        HostName = "xxxxxxxxxxxxxxxxxxxxxxxxx",
                        UserName = "xxxxxxxxxxxxxxxxxxxxxxxxx",
                        Password = "xxxxxxxxxxxxxxxxxxxxxxxxx",
                    };

                    using (Session session = new Session())
                    {
                        session.Open(sessionOptions);

                        const string externalPath = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                        const string PathLocal = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

                        RemoteDirectoryInfo directoryInfo = session.ListDirectory(externalPath);

    
                        RemoteFileInfo latest =
                            directoryInfo.Files
                                .Where(file => !file.IsDirectory)
                                .OrderByDescending(file => file.LastWriteTime)
                                .FirstOrDefault();

                        if (latest == null)
                        {
                            throw new Exception("Error, not found");
                        }

                        session.GetFiles(
                            externalPath.EscapeFileMask(latest.FullName), PathLocal).Check();
                    }

            }

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

这里发生了什么?

我做错了什么吗?

听起来像 TargetInvocationException。坏消息是:我们 无法告诉您潜在的错误是什么。好消息是运行时可以。除了所有异常,捕获它们并查看 inner 异常是个好主意。考虑:

try {
    // Your failing code
} catch (Exception ex) {
    while (ex is not null) {
        LogSomewhere($"{ex.Message} ({ex.GetType().Name})");
        ex = ex.InnerException;
    }
    // Todo: decide whether you want to rethrow ("throw") the
    // exception, vs consider it handled
}

您应该会发现这会输出额外的有用信息。

注意:其中 捕获异常有点难以定义 - 你不应该吞下异常,除非:

  • 您了解具体问题并已处理,或者
  • 您乐于在“尽最大努力”的基础上工作,或者
  • 你的带有 catch 的方法是顶级代码 - UI 事件处理程序或线程启动等;如果你不做某事,应用程序将终止

合理的,然而,捕获异常 log/inspect 它,如果你重新抛出,或者抛出一个新的更多 contextual/specific 异常(在在这种情况下,您可能应该将原始异常作为 您的 新异常的内部异常。