微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SSIS WinSCP C# 脚本任务正在​​运行但不执行任何操作

如何解决SSIS WinSCP C# 脚本任务正在​​运行但不执行任何操作

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

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

图片

Image

Image

enter image description here

这是我的代码

#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

    }
}

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


编辑:

enter image description here


编辑#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}",0);
    }
    
,

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

谢谢大家!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。