问题
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
I've done some digging and it looks like it has something to do with the reference to WinSCPnet.dll
.
回答1:
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.If you cannot access the inner exception easily, inspect WinSCP session log and debug log file (Session.SessionLogPath, Session.DebugLogPath). If those file are not even created, 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..
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 install the WinSCP .NET assembly.
You also need to install the assembly to the GAC or subscribe AppDomain.AssemblyResolve event 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.
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")
Absolute path to the DLL is required to be specified or the above method will fail (and the only indication of the failure is sent to Windows Event log).
回答2:
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:
- Copy the WinSCPnet assembly to your local computer into a folder. Start Visual Studio Command Prompt.
- Type the following command: gacutil.exe /if ""
- This installs the assembly to the GAC, overwriting any existing assembly that has the same assembly name.
回答3:
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.
来源:https://stackoverflow.com/questions/34345160/ssis-c-sharp-2012-script-task-referring-winscpnet-dll-fails-when-run-from-sql-se