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

调用的目标(SSIS&;WinSCP&;C#)引发了异常,c#,ssis,sftp,winscp,winscp-net,C#,Ssis,Sftp,Winscp,Winscp Net,我在尝试在SSIS中执行脚本任务时收到此错误消息(我使用的是VS2019和C#) 已经试了一段时间了,好像不行 错误消息:的目标已引发异常 调用 详细信息: in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) in System.Reflection.RuntimeMethodInfo.UnsafeInvoke

我在尝试在SSIS中执行脚本任务时收到此错误消息(我使用的是VS2019和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

    }
}

这是我的代码:

   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

    }
}
#区域名称空间
使用制度;
使用系统数据;
使用Microsoft.SqlServer.Dts.Runtime;
使用System.Windows.Forms;
使用System.Linq;
使用WinSCP;
#端区
命名空间ST_2cfb1a0997cf7b332da4453v3g1132g
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
公共部分类ScriptMain:Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
公共图书馆
{
SessionOptions SessionOptions=新SessionOptions
{
协议=协议.Sftp,
HostName=“xxxxxxxxxxxxxxxxxxxxx”,
UserName=“xxxxxxxxxxxxxxxxxxxxx”,
Password=“xxxxxxxxxxxxxxxxxxxxx”,
};
使用(会话=新会话())
{
会议。公开(会议选项);
常量字符串externalPath=“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
常量字符串PathLocal=@“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
RemoteDirectoryInfo directoryInfo=session.ListDirectory(externalPath);
RemoteFileInfo最新版本=
directoryInfo.Files
.Where(file=>!file.IsDirectory)
.OrderByDescending(文件=>file.LastWriteTime)
.FirstOrDefault();
如果(最新==null)
{
抛出新异常(“错误,未找到”);
}
session.GetFiles(
externalPath.EscapeFileTask(latest.FullName),PathLocal.Check();
}
}
#区域脚本结果声明
/// 
///这个枚举在这个类的范围内提供了一个方便的速记来设置
///脚本的结果。
/// 
///此代码是自动生成的。
/// 
枚举脚本结果
{
Success=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Success,
Failure=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Failure
};
#端区
}
}
这是怎么回事


我做错什么了吗?

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

试试看{
//你失败的代码
}捕获(例外情况除外){
while(ex不为空){
日志位置($“{ex.Message}({ex.GetType().Name})”;
ex=ex.InnerException;
}
//Todo:决定是否要重新播放(“抛出”)该文件
//例外,VS考虑处理
}
您应该会发现,这会输出更多有用的信息

注意:在何处捕获异常更难定义-除非:

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

但是,如果您重新调用或抛出新的更上下文相关/特定的异常(在这种情况下,您可能应该将原始异常作为新异常的内部异常),则捕获异常以记录/检查异常是合理的。

请参见+如图所示进行一些记录!我通过替换public void Main()解决了这个问题。我使用了public int Main(),它没有显示任何错误,但工作不正常:@programming\u令人惊讶的是,它没有“解决”任何问题;再一次,您需要记录内部异常-它已经试图告诉您问题是什么,但您忽略了它Marc,我能够打印错误消息,错误本身是另一回事,但您的回答对我帮助很大!。谢谢