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 try
… catch
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
- See How to fix NuGet WinSCP.NET in SSIS Script Task?
- Check if
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WinSCPnet
folder containing the assembly was created.