SSIS WinSCP C# 脚本任务 运行 但没有执行任何操作

SSIS WinSCP C# script task running but not doing anything

我的代码没有抛出任何错误,但它根本没有做任何事情。

我正在尝试连接到 SFTP 服务器,任务本身没有显示任何错误,但它不是 accessing/getting 存储在特定目录中的文件。

图片:

这是我的代码:

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

namespace ST_t5fbgt5564cf3a165da70892d8c435v
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public int Main()
        {
            try
            { 
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                UserName = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                Password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                PortNumber = xx
            };

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

                const string remotePath = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                const string localPath = @"C:\xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

                RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

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

                if (latest == null)
                {
                    throw new Exception("No found");
                }

                session.GetFiles(
                    RemotePath.EscapeFileMask(latest.FullName), localPath).Check();
            }
                return 0;
        }
            catch (Exception e)
            {
                Console.WriteLine("Error: (0)", e);
                return 1;

            }
            }


        #region ScriptResults declaration
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

我的代码有什么问题吗?我错过了什么吗?


编辑:


编辑 #2

我能够打印日志消息,这是错误:

Error: Error: WinSCP.SessionLocalException: The version of C:\Program Files (x86)\WinSCP\winscp.exe (5.15.3.0) does not match version of this assembly C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WinSCPnet\v4.0_1.7.2.11087__2271ec4a3c56d0bf\WinSCPnet.dll (5.17.10.0).
   in WinSCP.ExeSessionProcess.CheckVersion(String exePath, FileVersionInfo assemblyVersion)
   in WinSCP.ExeSessionProcess..ctor(Session session, Boolean useXmlLog, String additionalArguments)
   in WinSCP.Session.Open(SessionOptions sessionOptions)
   in ST_2u7fsdf8fdsfkjgd998fsdf9ss.ScriptMain.Main()

我通过下载正确版本的应用程序解决了这个问题。

谢谢大家!

您问题的实际答案是:做一些日志记录!

WinSCP SSIS example:

  • 经常使用 Dts.Events.FireInformation

  • 在整个代码周围放置 try/catch 并记录所有异常:

    try
    {
        // The code
    }
    catch (Exception e)
    {
        Dts.Events.FireError(
            0, null, $"Error when using WinSCP to upload files: {e}", null, 0);
    }