4

I have an SSIS package (created in VS 2013) that contains a C# 2012 Script Task.

The job of the script task is to download a file from an SFTP server using WinSCP .NET assembly and place it on my server (Windows Server 2012 R2 with SQL Server 2014)

My package runs fine when I run it on my Dev machine, but when I deploy to the server my package fails at this task with the error message

Exception has been thrown by the target of an invocation

enter image description here

I've done some digging and it looks like it has something to do with the reference to WinSCPnet.dll.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
steveman2292
  • 53
  • 1
  • 7

3 Answers3

2

Quoting WinSCP article on Exception has been thrown by the target of an invocation:

This is just a high-level exception. The root cause is usually stored in the InnerException.

If you are getting this exception in SSIS, you can use trycatch block to capture the error, as show in the example for using WinSCP .NET Assembly from SSIS. Though this won't help if the error occurs even before the SSIS Main method is started. In this case, the actual error should be shown on the Progress tab of your SSIS package. Often, the root cause can be loading of WinSCPnet.dll assembly. See Could not load file or assembly ‘file:///…\WinSCPnet.dll’ or one of its dependencies. The system cannot find the file specified.

If you do not use SSIS and you cannot access the inner exception easily, inspect WinSCP session log and debug log file (Session.SessionLogPath, Session.DebugLogPath).


Installing the assembly to allow its loading is covered in Installing section of Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS):

Installing

First, you need to download the WinSCP .NET assembly. Do not use the NuGet package.1

You also need to install the assembly to the GAC or subscribe AppDomain.AssemblyResolve event in your code to allow loading the assembly.


The installation to GAC is covered by Installing to GAC section of Installation instructions for WinSCP .NET assembly:

Installing to GAC

In special cases, you may need to install the assembly into Global Assembly Cache (GAC), particularly to use it from SSIS.

When you install the assembly to GAC, you need to configure a path to WinSCP executable.

In many cases, instead of using GAC, you can subscribe AppDomain.AssemblyResolve event.

On Development Machine

To install the assembly into GAC on development machine, i.e. the one that has Windows SDK installed, use following command:

gacutil.exe /i WinSCPnet.dll

Windows SDK comes with Microsoft Visual Studio. You can also install it separately.

Use correct gacutil.exe for your version of .NET framework:

  • For .NET framework 4.0 or newer, use gacutil from Windows SDK 7.1 (or newer):
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1\bin\gacutil.exe;
  • For .NET framework 3.5, use gacutil from Windows SDK 6.0:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Bin\gacutil.exe

On Production or User's Machine

To install the assembly into GAC on production or user’s machine, you may install the assembly into GAC using:

  • Windows Installer, by creating .msi package;

  • Any other installer system that supports installing to GAC, e.g. Inno Setup;

  • System.EnterpriseServices.Internal.Publish.GacInstall method. PowerShell example:

    Add-Type -AssemblyName "System.EnterpriseServices"
    $publish = New-Object System.EnterpriseServices.Internal.Publish
    $publish.GacInstall("WinSCPnet.dll")
    

    An absolute path to the DLL needs to be specified and Administrator privileges are required. Otherwise the above method will fail (and the only indication of the failure is sent to Windows Event log).2


  1. See How to fix NuGet WinSCP.NET in SSIS Script Task?
  2. Check if C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WinSCPnet folder containing the assembly was created.
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

You need to install WinSCPnet.dll in the Global Assembly Cache for SSIS to pick up this dependency, see this blog for further details. You can install a DLL in the GAC using the following steps, see the documentation for further details:

  1. Copy the WinSCPnet assembly to your local computer into a folder. Start Visual Studio Command Prompt.
  2. Type the following command: gacutil.exe /if ""
  3. This installs the assembly to the GAC, overwriting any existing assembly that has the same assembly name.
Alex
  • 21,273
  • 10
  • 61
  • 73
  • would the .dll not being in the GAC stop the package from running in Visual Studios? – steveman2292 Dec 18 '15 at 13:38
  • i just installed gacutil on the server and ran the "/l WinSCPnet" command and it turns out the .dll is already installed on the GAC, so the issue must be something else – steveman2292 Dec 18 '15 at 14:00
1

If you have already registered the appropriate Dll's are are still getting this error, and you are passing in variables that are package-scoped, be sure to add them to the "ReadOnlyVariables" in the script task.

Right click on the task and click "Edit...", then click the "..." on the "ReadOnlyVariables" property, and add the variables that are referenced in your script.

nh43de
  • 813
  • 11
  • 11