使用winscp将文件上传到sFTP的脚本导致错误

时间:2016-11-30 06:18:00

标签: c# ssis

我收到了这个错误:

  

错误:    at System.RuntimeMethodHandle.InvokeMethod(Object target,Object [] arguments,Signature sig,Boolean constructor)      at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object [] parameters,Object [] arguments)      在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)      at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)      在Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

代码:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Renci.SshNet;
using System.IO;
using System.Threading;
#endregion

namespace ST_e4fe7cc5f9914a52b66a9e0bb572fa3b
{

    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {


        public void Main()
        {
            var UserName = Dts.Variables["User::SFTPUserName"].Value.ToString();
            var HostName = Dts.Variables["User::SFTPServerName"].Value.ToString();
            var KeyFilePath = Dts.Variables["User::SFTPKeyFilePath"].Value.ToString();
            var UploadPath = Dts.Variables["User::SFTPDirectoryPath"].Value.ToString();
            var OutBoundFilePath = Dts.Variables["User::OutBoundFilePath"].Value.ToString() ;
            int RetryInterval = Convert.ToInt32(Dts.Variables["User::RetryInterval"].Value.ToString());
            int RetryLimit = Convert.ToInt32(Dts.Variables["User::RetryLimit"].Value.ToString());

            //Create Connection   
            PrivateKeyFile objKeyFile = new PrivateKeyFile(KeyFilePath);
            SftpClient objSFTPclient = new SftpClient(HostName, UserName, objKeyFile);
            objSFTPclient.BufferSize = 32000;

            //Upload Files
            try
            {
                objSFTPclient.Connect();
                UploadFiles(objSFTPclient, UploadPath, OutBoundFilePath);
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch (Exception ex)
            {
                //An error occurred.
                Dts.Events.FireError(0, "SFTP", ex.Message + "\r" + ex.StackTrace, String.Empty, 0);
                Dts.TaskResult = (int)ScriptResults.Failure;
                //throw;
            }

            finally
            {
                if (objSFTPclient != null && objSFTPclient.IsConnected)
                {
                    objSFTPclient.Disconnect();
                    objSFTPclient.Dispose();
                }
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }
        public void UploadFiles(SftpClient objSFTPclient, String UploadPath, String OutBoundFilePath)
        {
            string FTPFolderName = Dts.Variables["User::varRRODataSource"].Value.ToString();

            if (!(objSFTPclient.Exists(UploadPath + "/" + FTPFolderName)))
            {
                objSFTPclient.CreateDirectory(UploadPath + "/" + FTPFolderName);
            }

            if (objSFTPclient.Exists(UploadPath + "/" + FTPFolderName))
            {
                if (bool.Parse(Dts.Variables["User::EncryptDecryptEnabled"].Value.ToString()))
                {
                    var fs = new FileStream(OutBoundFilePath + "\\" + Dts.Variables["User::CompressedFileName"].Value.ToString() + ".zip.aes", FileMode.Open);
                    objSFTPclient.UploadFile(fs, UploadPath + "//" + FTPFolderName + "//" + Dts.Variables["User::CompressedFileName"].Value.ToString() + ".zip.aes");



                    System.IO.File.Create(OutBoundFilePath + "\\done.txt").Close();
                    using (var fs1 = File.OpenRead(OutBoundFilePath + "\\done.txt"))
                    {
                        objSFTPclient.UploadFile(fs1, UploadPath + "//" + FTPFolderName + "//done.txt");
                    }
                }
                else
                {
                    var fs = new FileStream(OutBoundFilePath + "\\" + Dts.Variables["User::CompressedFileName"].Value.ToString() + ".zip", FileMode.Open);
                    objSFTPclient.UploadFile(fs, UploadPath + "/" + FTPFolderName + "/" + Dts.Variables["User::CompressedFileName"].Value.ToString() + ".zip");
                    objSFTPclient.Create(UploadPath + "/" + FTPFolderName +      "/done.txt");
                }
            }
        }

        #region ScriptResults declaration

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

    }
}

1 个答案:

答案 0 :(得分:0)

@Arumugam, 在脚本任务中,输入断点。如果您在第一次尝试/捕获之前失败,请将第一个断点放在这一行:

SftpClient objSFTPclient = new SftpClient(HostName,UserName,objKeyFile);

如果您发布的堆栈跟踪发生在托盘/ catch中,请将第一个断点放在此行上:

objSFTPclient.Connect();

我不是Renci.SshNet专家,但似乎代码试图通过SSIS打开shell,因此调用失败。如果Renci.SshNet有一个用于sftp文件传输的命令行 - 这就是你尝试做的事情,那就使用执行进程任务。