2016-11-30 75 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文件傳輸的命令行 - 這看起來就像你正在嘗試做的那樣,使用那個利用執行進程任務的命令行。