Shaun Neal
Shaun Neal

Reputation: 1193

Winscp with SSIS package gives System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)

Installed WinSCP .net using nuget installer.

Visual Studio 2013

SSIS BIDS 2012

Project references are correct - pointing to DLL that was installed

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.

registered winscpnet.dll in GAC per instructions.

start script in visual studio ssis debugger, get this:

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()

    public void Main()
    {

        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            // To setup these variables, go to SSIS > 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;
    }

Adding screencaps of the flow and process

Heres the package Script details Error when I run it Debug spew

UPDATE: Modified the code as follows...exact same result

        public void Main()
    {
        bool fireAgain = false;

        try
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                // To setup these variables, go to SSIS > 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;
    }

Upvotes: 10

Views: 20574

Answers (3)

Fred Montoya
Fred Montoya

Reputation: 41

I know this is an old issue but hopefully this helps. Basically what you need is this chunk of code outside of the public void main.

public class example
{
    static ScriptMain()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.Name.Contains("WinSCPnet"))
        {
            string path = @"Path to DLL";
            return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "WinSCPnet.dll"));
        }
        return null;
    }
    public void Main()
    { can now use DLL things in here}
}

do add the using WinSCP; and add it to your references of course. Good luck.

Upvotes: 2

Rahul Sonone
Rahul Sonone

Reputation: 2725

I got this same Exception at C# delegate event invocation, after digging it with try catch block, and with help of stacktrace, I found that there was one exception thrown from one of the event handler methods. after fixing that it started working fine.

Upvotes: 0

Nicky Thakkar
Nicky Thakkar

Reputation: 141

When third party DLL is used in SSIS Script Task we need to perform GAC.

Please open Command prompt.

cd "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools"

Run Following command.

gacutil -i <"Path of WinSCP DLL">

After running the GAC command Scrip task should run as expected. At runtime SSIS is unable to get DLL reference and that is the cause for this error.

Hope this works!!

Upvotes: 14

Related Questions