title: Winscp with SSIS package gives System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
body: <p>Installed WinSCP .net using nuget installer.</p>
  
  <p>Visual Studio 2013</p>
  
  <p>SSIS BIDS 2012</p>
  
  <p>Project references are correct - pointing to DLL that was installed</p>
  
  <p>Project contains one script which is a stripped down version of the sample code from the winscp site.  Fails on the first line which tries to instantiate SessionOptions object.  If I remove SessionOptions object it's fine.</p>
  
  <p>registered winscpnet.dll in GAC per instructions.</p>
  
  <p>start script in visual studio ssis debugger, get this:</p>
  
  <blockquote>
    <p>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)<br>
    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()</p>
  </blockquote>
  
  <pre><code>    public void Main()
      {
  
          SessionOptions sessionOptions = new SessionOptions
          {
              Protocol = Protocol.Sftp,
              // To setup these variables, go to SSIS &gt; Variables.
              // To make them accessible from the script task, in the context menu of the task,
              // choose Edit. On the Script task editor on Script page, select ReadOnlyVariables,
              // and tick the below properties.
              HostName = "",
              UserName = "",
              Password = "",
              SshHostKeyFingerprint = ""
          };
  
              bool fireAgain = false;
  
           Dts.Events.FireInformation(0, null,
                          string.Format("Upload of  succeeded"),
                          null, 0, ref fireAgain);
  
  
  
              Dts.TaskResult = (int)DTSExecResult.Success;
      }
  </code></pre>
  
  <p>Adding screencaps of the flow and process</p>
  
  <p><img src="https://i.stack.imgur.com/i8xdU.png" alt="Heres the package">
  <img src="https://i.stack.imgur.com/xsjP6.png" alt="Script details">
  <img src="https://i.stack.imgur.com/crpQJ.png" alt="Error when I run it">
  <img src="https://i.stack.imgur.com/uMggZ.png" alt="Debug spew"></p>
  
  <p>UPDATE: Modified the code as follows...exact same result</p>
  
  <pre><code>        public void Main()
      {
          bool fireAgain = false;
  
          try
          {
              SessionOptions sessionOptions = new SessionOptions
              {
                  Protocol = Protocol.Sftp,
                  // To setup these variables, go to SSIS &gt; Variables.
                  // To make them accessible from the script task, in the context menu of the task,
                  // choose Edit. On the Script task editor on Script page, select ReadOnlyVariables,
                  // and tick the below properties.
                  HostName = "",
                  UserName = "",
                  Password = "",
                  SshHostKeyFingerprint = ""
              };
          }
          catch (Exception ex)
          {
  
              Dts.Events.FireInformation(0, null,
                             ex.InnerException.Message,
                             null, 0, ref fireAgain);
          }               
  
              Dts.TaskResult = (int)DTSExecResult.Success;
      }
  </code></pre>