在SSIS脚本任务静态构造函数中访问可配置的值 [英] Accessing a Configurable Value in an SSIS Script Task Static Constructor

查看:125
本文介绍了在SSIS脚本任务静态构造函数中访问可配置的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SSIS程序包,其中包含一个脚本任务,该脚本任务需要一个第三方程序集.由于不允许将该程序集放置在SSIS服务器上的GAC中,因此我在运行时将程序集绑定到脚本任务的静态构造函数中.此

I have an SSIS package with a script task that needs a 3rd party assembly. Since I am not allowed to place this assembly in the GAC on the SSIS server, I am binding the assembly at run time in the static constructor of the script task. This article is what I used as a guideline. However I want to find a way to avoid hardcoding the path to the assembly file.

我的工作代码如下:

  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对象尚未实例化,因此无法访问包变量.

1) Setting the path as a package variable. This doesn't work because the Dts object is not yet instantiated when the static constructor runs so there is no access to the package variables.

2)尝试访问正在触发程序集解析事件的应用程序域,如下所示:

2) Tried accessing the app domain that is firing the assembly resolve event like this:

string appDomainPath = ((AppDomain)sender).BaseDirectory;

但这只是获取VSTA代码所在的目录.

But this just gets the directory where the VSTA code lives.

我没主意了.这有可能吗?

I'm out of ideas. Is this even possible?

推荐答案

可以使用Environment将包变量潜入静态构造函数中:

It is possible to make use of Environment to sneak a package variable into the static constructor:

在初始化"脚本任务中,Main():

In an "Initialization" Script Task, Main():

Environment.SetEnvironmentVariable("LIBRARY_PATH", Dts.Variables["$Package::LIBRARY_PATH"].Value.ToString(), EnvironmentVariableTarget.Process);

然后在将来的脚本组件/任务中的任何地方

Then anywhere in future Script Component/Task

        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string library_path = Environment.GetEnvironmentVariable("LIBRARY_PATH", EnvironmentVariableTarget.Process);
            /* ...
               ... */
        }

注意:

  • 首选使用包变量,因为它可以根据环境进行覆盖并通过SQL Server代理进行设置:
  • 在不需要组装的初始Task中,我们将路径粘贴到EnvironmentVariable中,并使用具有DTS访问权限的Main()限制进程(以防止泄漏到其他并发执行中).
  • 仅此而已,现在您可以收获更多...我们的库路径将在其他所有下游任务中的静态构造函数中访问.

这篇关于在SSIS脚本任务静态构造函数中访问可配置的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆