繁体   English   中英

调用的目标已引发异常(SSIS & WinSCP & C#)

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

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

试了一段时间好像不行

错误消息:调用的目标已引发异常

细节:

   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 坏消息是:我们无法告诉您潜在的错误是什么。 好消息是运行时可以 对于所有异常,最好捕获它们并查看内部异常。 考虑:

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 事件处理程序或线程启动等; 如果你不做某事,应用程序将终止

但是,捕获异常以记录/检查它合理的,如果您重新抛出或抛出一个新的更具上下文/特定的异常(在这种情况下,可能应该将原始异常包含为新异常的内部异常) .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM