无法在SSIS脚本任务中使用变量

时间:2016-08-08 18:49:31

标签: c# json dll ssis

我有一个SSIS包,它使用脚本任务来动态加载DLL。当我将文件路径硬编码到字符串变量path

时,它可以工作

WORKING

namespace ST_0a8ce4d87e1c40b4a7bbf7bd3be96cbc.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

    #region VSTA generated code
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion

    public ScriptMain()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }

    public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.Name.Contains("Newtonsoft"))
        {
            string path = @"\\path\to\DLL\Newtonsoft.Json.dll";
            return Assembly.LoadFile(path);
        }
        return null;
    }

    public void Main()
    {
        // TODO: Add your code here
        Dts.TaskResult = (int)ScriptResults.Success;
    }
}

}

但是当我尝试更换这条线时:

string path = @"\\path\to\DLL\Newtonsoft.Json.dll";

使用:

string path = Dts.Variables["User::FilePath_JsonDLL"].Value.ToString();

如果变量值为\\path\to\DLL\Newtonsoft.Json.dll,则包出错并说它可以加载文件或程序集'Newtonsoft.Json'?我不确定为什么这可以使用硬编码值而不是变量?

更新 - 添加错误消息

    Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
    File name: 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' ---> Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
 ---> System.Runtime.InteropServices.COMException (0xC0010009): The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.

   at Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSVariables100.get_Item(Object Index)
   at Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   at ST_0a8ce4d87e1c40b4a7bbf7bd3be96cbc.csproj.ScriptMain.CurrentDomain_AssemblyResolve(Object sender, ResolveEventArgs args)
   at System.AppDomain.OnAssemblyResolveEvent(String assemblyFullName)
   at ST_6d9e84ae1c284668ac5306f36b3f5f36.csproj.ScriptMain.Main()


   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   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()

0 个答案:

没有答案