简体   繁体   English

SSIS 脚本任务以编程方式引用 dll

[英]SSIS Script Task reference dll programmatically

I have built software that is capable of exporting DTSX package automatically.我已经构建了能够自动导出 DTSX 包的软件。 This package among other objects has also a ScriptTask (C#).这个包和其他对象还有一个 ScriptTask (C#)。 All are compiled and run just fine.所有这些都被编译并运行得很好。

Now the new requirement is to call a class in that ScriptTask, which exists inside an external DLL we have built, so other applications can consume the same code.现在新的要求是调用 ScriptTask 中的一个类,它存在于我们构建的外部 DLL 中,以便其他应用程序可以使用相同的代码。 We did our homework, and we included this DLL into the GAC successfully during the installation of the software.我们做了功课,在安装软件的过程中,我们成功地将这个DLL包含到了GAC中。

The problem is that "using our library" is still not recognized in the script.问题是脚本中仍然无法识别“使用我们的库”。

While searching a little bit, we figured out, that we need to reference this DLL also inside the References folder.在稍微搜索时,我们发现,我们还需要在 References 文件夹中引用此 DLL。 This we can do it of course via DataTools / VisualStudio UI.这当然可以通过 DataTools / VisualStudio UI 来完成。

The issue is that we need to do that programmatically:问题是我们需要以编程方式做到这一点:

We have this piece of code that generates the Project我们有这段代码可以生成项目

task.ScriptingEngine.VstaHelper.LoadNewProject(task.ProjectTemplatePath, null, "MyScriptProject");

And also, we have this piece of code that creates the MainScript而且,我们有这段代码可以创建 MainScript

task.ScriptingEngine.VstaHelper.AddFileToProject(ScriptName + ".cs", MainScript.ToString());

I am unable to figure out how I can include the reference DLL programmatically.我无法弄清楚如何以编程方式包含参考 DLL。

Updated Answer更新答案

You can programmatically update the script task by replacing the appropriate XML node in the the DTSX file您可以通过替换 DTSX 文件中的相应 XML 节点,以编程方式更新脚本任务

The node path depends on where the script task has been created within the SSIS package, in my case the node path was节点路径取决于在 SSIS 包中创建脚本任务的位置,在我的情况下,节点路径是

/DTS:Executable/DTS:Executables/DTS:Executable/DTS:ObjectData/pipeline/components/component[@refId="Package\\Data Flow Task\\Script Component"]/properties

The @refId you will be looking for will start with Package \\ Dataflow name \\ Component name您要查找的 @refId 将以 Package \\ Dataflow name \\ Component name 开头

在此处输入图片说明

This node will have sub nodes which contains the C# scripts as well as the binary that was built off this script此节点将具有包含 C# 脚本以及基于此脚本构建的二进制文件的子节点

The property name "SourceCode" contains the C# scipts in an array called arrayElements, the array will have three sub nodes for each file, these subnodes are called arrayElement, first value is the relative path and name, second is file encoding and third is the file content属性名称“SourceCode”包含一个名为 arrayElements 的数组中的 C# scipts,该数组将有每个文件的三个子节点,这些子节点称为 arrayElement,第一个值是相对路径和名称,第二个是文件编码,第三个是文件内容

源代码节点

The property name "BinaryCode" contains the .dll that was build from the scripts, it also contains an arrayElement array with two entries, first the dll name and the second the base64 encoded dll binary属性名称“BinaryCode”包含从脚本构建的 .dll,它还包含一个包含两个条目的 arrayElement 数组,第一个是 dll 名称,第二个是 base64 编码的 dll 二进制文件

二进制代码节点

To get the data to populate these items you will need to create a template of the C# build directory, apply your changes, build the code and take the resulting files and replace them on their appropriate nodes要获取填充这些项目的数据,您需要创建 C# 构建目录的模板、应用更改、构建代码并获取生成的文件并在相应的节点上替换它们

To create the template open the script via SSIS task,要创建模板,请通过 SSIS 任务打开脚本,

  1. Click on the project in solution explorer to and go file save VstaProject.sln单击解决方案资源管理器中的项目并转到文件保存 VstaProject.sln 保存SLN
  2. Go to the saved folder, you get the folder off the solutions properties dialog转到保存的文件夹,您可以从解决方案属性对话框中获取该文件夹查找文件夹
  3. Copy this folder somewhere so that you can reuse it to build your custom stuff将此文件夹复制到某处,以便您可以重复使用它来构建您的自定义内容
  4. Modify the .cs files in your template directory and add your custom reference dll's to your .csproj file修改模板目录中的 .cs 文件,并将自定义引用 dll 添加到 .csproj 文件中
  5. Call MSBUILD in the same directory as your .csproj to output the dll, its important that you use the VS MSBUILD, VS2017 you can find it in调用 .csproj 所在目录下的 MSBUILD 输出 dll,重要的是你使用的是 VS MSBUILD,VS2017 你可以在

C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\msbuild.exe

  1. Take these files and package them into a XML node in the DTSX file把这些文件打包成DTSX文件中的一个XML节点

Initial Answer初步答复

Microsoft provides a workaround for loading DLL's that aren't in the GAC Microsoft 提供了一种解决方法来加载不在 GAC 中的 DLL

Load assembly that isnt in the GAC 加载不在 GAC 中的程序集

Please see below extract from a SSIS script, I loaded the JSON dll's from the nuget install directory.请参阅以下 SSIS 脚本的摘录,我从 nuget 安装目录加载了 JSON dll。 This DLL is not in the GAC此 DLL 不在 GAC 中

static ScriptMain()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.Contains("Newtonsoft.Json"))
    {
        string path = @"C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\";
        return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.Json.dll"));
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM