简体   繁体   English

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

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

I'm getting this error message while trying to execute my SCRIPT TASK in SSIS (I'm using VS 2019 and C#).我在尝试在 SSIS 中执行我的 SCRIPT TASK 时收到此错误消息(我使用的是 VS 2019 和 C#)。

Been trying for a while it seems it wont work.试了一段时间好像不行

Error message: Exception has been thrown by the target of an invocation错误消息:调用的目标已引发异常

Details:细节:

   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()

This is my code:这是我的代码:

#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

    }
}

What is going on in here?这是怎么回事?

Am i doing anything wrong?我做错什么了吗?

That sounds like TargetInvocationException .这听起来像TargetInvocationException The bad news is: we can't tell you what the underlying error was.坏消息是:我们无法告诉您潜在的错误是什么。 The good news is the runtime can .好消息是运行时可以 With all exceptions, it is a good idea to catch them and look at the inner exception(s).对于所有异常,最好捕获它们并查看内部异常。 Consider:考虑:

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
}

You should find that this outputs additional useful information.您应该会发现这会输出额外的有用信息。

Note: where to catch exceptions is a bit harder to define - you shouldn't swallow an exception unless:注意:在哪里捕获异常有点难以定义——你不应该吞下一个异常,除非:

  • you understand the specific problem and have handled it, or您了解具体问题并已处理,或
  • you are happy to just work on a "best efforts" basis, or你很乐意在“尽力而为”的基础上工作,或者
  • your method with a catch is top-level code - a UI event-handler, or a thread-start, etc;带有catch的方法是顶级代码 - UI 事件处理程序或线程启动等; if you don't do something , the application will terminate如果你不做某事,应用程序将终止

It is reasonable, however, to catch an exception to log/inspect it, if you rethrow, or throw a new more contextual/specific exception (in which case you should probably include the original exception as the inner-exception of your new exception).但是,捕获异常以记录/检查它合理的,如果您重新抛出或抛出一个新的更具上下文/特定的异常(在这种情况下,可能应该将原始异常包含为新异常的内部异常) .

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

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