Search code examples
c#ssissftpwinscpscript-task

SSIS WinSCP C# script task running but not doing anything


My code is not throwing any error but it is not doing anything at all.

I'm trying to connect to a SFTP server, the task itself is not showing any error but it is not accessing/getting the file stored in a specific directory.

Images:

Image

Image

enter image description here

This is my code:

#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

    }
}

Is anything wrong with my code? am i missing anything?


EDIT:

enter image description here


EDIT #2

I was able to print log messages and this is the error:

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

Solution

  • The actual answer to your question is: Do some logging!

    See the the WinSCP SSIS example:

    • Use Dts.Events.FireInformation a lot.

    • Put try/catch around your whole code and log all exceptions:

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