2016-11-30 74 views
0

我得到这个错误上传文件到SFTP:脚本使用WinSCP赋予导致错误

Error: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at 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

请在您的问题中添加更多信息。 [我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask) –

+0

嗨莱昂,我执行此通过SSIS数据工具,当代码命中这个脚本步骤它给了我错误,我上面提到 –

回答

0

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

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

如果您发布的堆栈跟踪托盘/ catch语句内发生,请将您的第一个断点在这条线:

objSFTPclient.Connect();

我不是Renci.SshNet专家,但它似乎代码试图通过SSIS打开一个shell,所以调用失败。如果Renci.SshNet有一个用于sftp文件传输的命令行 - 这看起来就像你正在尝试做的那样,使用那个利用执行进程任务的命令行。