我有一个 SSIS 包,其中包含需要第 3 方程序集的脚本任务。由于不允许我将此程序集放置在 SSIS 服务器上的 GAC 中,因此我在运行时在脚本任务的静态构造函数中绑定该程序集。这article https://blogs.msdn.microsoft.com/dbrowne/2014/06/25/how-to-load-an-assembly-in-a-ssis-script-task-that-isnt-in-the-gac/这是我用作指导的内容。但是我想找到一种方法来避免对程序集文件的路径进行硬编码。
我的工作代码如下所示:
static ScriptMain()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("thirdparty"))
{
string path = @"C:\mydrive\Solution\Reference";
return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "thirdparty.dll"));
}
return null;
}
我尝试过的:
1) 将路径设置为包变量。这不起作用,因为静态构造函数运行时 Dts 对象尚未实例化,因此无法访问包变量。
2)尝试访问正在触发程序集解析事件的应用程序域,如下所示:
string appDomainPath = ((AppDomain)sender).BaseDirectory;
但这只是获取 VSTA 代码所在的目录。
我没主意了。这可能吗?
可以利用环境将包变量潜入静态构造函数中:
在“初始化”脚本任务中,Main():
Environment.SetEnvironmentVariable("LIBRARY_PATH", Dts.Variables["$Package::LIBRARY_PATH"].Value.ToString(), EnvironmentVariableTarget.Process);
然后是未来脚本组件/任务中的任何地方
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string library_path = Environment.GetEnvironmentVariable("LIBRARY_PATH", EnvironmentVariableTarget.Process);
/* ...
... */
}
Notes:
- 首选使用包变量,因为它可以根据环境进行覆盖并通过 SQL Server 代理进行设置:
- 在不需要程序集的初始任务中,我们使用具有 DTS 访问权限的 Main() 将路径粘贴到环境变量中,其范围仅限于进程(以防止泄漏到其他并发执行中)。
- 就这样,现在收获成果......我们的库路径将可以在每个其他下游任务的静态构造函数中访问。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)