SSIS 2022中.NET Framework项目加载System.Runtime 6.0.0.0遇FileLoadException
SSIS 2022脚本组件加载System.Runtime 6.0.0.0时触发FileLoadException
问题详情
运行SSIS 2022脚本组件时,遇到FileLoadException,错误信息如下:Could not load assembly l'assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
包中包含Azure Blob附件上传逻辑,所有Azure Blob相关NuGet包已更新至最新版本。
环境信息
- 目标框架:.NET Framework 4.7
- 开发环境:Visual Studio 2022
- 操作系统:Windows 11
已尝试手动在项目app.config中添加以下程序集绑定重定向,但无效果:
<dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> </dependentAssembly>
解决方案
1. 修正程序集绑定重定向的版本指向
当前重定向指向.NET 6的System.Runtime版本,但项目基于.NET Framework 4.7,需改为绑定到兼容的.NET Framework版本:
修改app.config中的重定向配置为:
<dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.2.2.0"/> </dependentAssembly>
2. 修改SSIS运行时的配置文件
SSIS脚本组件由DTSHost.exe托管运行,需修改对应路径下的配置文件:
- 64位环境:编辑
C:\Program Files\Microsoft SQL Server\160\DTS\Binn\DTSHost.exe.config - 32位环境:编辑
C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn\DTSHost.exe.config
在文件的<runtime>节点下添加上述修正后的绑定重定向代码。
3. 降级Azure Blob NuGet包版本
最新版Azure.Storage.Blobs默认依赖.NET 6+组件,与.NET Framework 4.7兼容性差。建议降级到兼容的稳定版本,比如12.14.1:
- 在NuGet包管理器中搜索
Azure.Storage.Blobs - 选择版本12.14.1进行安装
4. 配置脚本组件的程序集复制属性
在脚本组件的项目引用中找到System.Runtime:
- 右键选择“属性”
- 将“复制本地”设置为
True,确保该程序集被复制到包运行的输出目录
5. 代码中添加程序集解析逻辑
在脚本组件的ScriptMain类中添加静态构造函数和程序集解析事件,强制加载正确版本:
static ScriptMain() { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; } private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.StartsWith("System.Runtime, Version=6.0.0.0")) { // 替换为你项目中System.Runtime.dll的实际路径 return Assembly.LoadFrom(@"C:\YourProjectPath\bin\Debug\System.Runtime.dll"); } return null; }
内容的提问来源于stack exchange,提问作者sadzag
相关产品推荐
相关产品推荐

