首页 > 解决方案 > 将 newtonsoft 安装到全局程序集缓存以用于 SSIS 脚本任务

问题描述

我正在使用 newtonsoft dll 来解析 SSIS 脚本任务中的 json 数据。这在我的本地计算机上按预期工作,但是当部署到 SQL Server 并从 SQL Server 作业运行时会抛出错误:

“调用的目标已引发异常”

我正在尝试将 newtonsoft dll 安装到全局程序集缓存。为了做到这一点,我必须将 Newtonsoft 安装到服务器上,例如它保存到 C 目录。然后使用 gacutil 将其安装到全局程序集缓存。

 C:\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll
    
 gacutil /i Newtonsoft.Json.dll

在脚本任务 C# 代码中引用下面的 dll?

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

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{          
    string path = @"C:\Newtonsoft.Json.12.0.3\lib\net45\";
    return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.Json.dll"));
}

标签: c#ssisjson.netgac

解决方案


如果您使用Reflection,则不需要在 GAC 中安装它,您只需要确保 DLL 位于服务器上与您在代码中引用的路径相同的路径中

C:\Newtonsoft.Json.12.0.3\lib\net45\


推荐阅读