使用SSIS脚本任务C#代码通过服务主体认证连接Azure SQL MI
解决SSIS脚本任务中服务主体连接Azure SQL MI的DLL依赖问题
问题背景
我需要通过SSIS脚本任务的C#代码,以服务主体认证方式连接Azure SQL MI服务器,当前环境:
- Microsoft.Data.SqlClient.dll v5.0.0
- .NET Framework v4.7(默认)
- Visual Studio 2019
- 目标Azure SQL MI版本2019
已将Microsoft.Data.SqlClient.dll注册到GAC,并复制了所有依赖DLL到指定路径并在代码中引用,依赖列表如下:
Microsoft.IdentityModel.Abstractions.6.21.0 Microsoft.Identity.Client.4.45.0 Microsoft.Identity.Client.Extensions.Msal.2.19.3 Microsoft.IdentityModel.Logging.6.21.0 Microsoft.IdentityModel.Tokens.6.21.0 Microsoft.IdentityModel.JsonWebTokens.6.21.0 Microsoft.IdentityModel.Protocols.6.21.0 System.Buffers.4.5.1 System.IdentityModel.Tokens.Jwt.6.21.0 Microsoft.IdentityModel.Protocols.OpenIdConnect.6.21.0 System.IO.4.3.0 System.Numerics.Vectors.4.5.0 System.Runtime.4.3.0 System.Runtime.CompilerServices.Unsafe.4.7.1 System.Memory.4.5.4 System.Diagnostics.DiagnosticSource.4.6.0 System.Runtime.InteropServices.RuntimeInformation.4.3.0 System.Security.Cryptography.Encoding.4.3.0 System.Security.Cryptography.Primitives.4.3.0 System.Security.Cryptography.Algorithms.4.3.1 System.Security.Cryptography.ProtectedData.4.7.0 System.Security.Cryptography.X509Certificates.4.3.0 System.Net.Http.4.3.4 System.Security.Principal.Windows.5.0.0 System.Security.AccessControl.5.0.0 System.Security.Permissions.5.0.0 System.Configuration.ConfigurationManager.5.0.0 System.Text.Encodings.Web.4.7.2 System.Threading.Tasks.Extensions.4.5.4 Microsoft.Bcl.AsyncInterfaces.1.1.1 System.ValueTuple.4.5.0 System.Text.Json.4.7.2 System.Memory.Data.1.0.2 Azure.Core.1.24.0 Azure.Identity.1.6.0 Microsoft.Data.SqlClient.5.0.0
但运行SSIS脚本任务时仍报错:Error: Object reference not set to an instance of an object。相同C#代码在普通.NET项目中可正常运行,因此排除代码问题,推测是SSIS脚本任务中的DLL部署或加载问题。
可能的解决方法
1. 确认DLL放置路径正确
SSIS脚本任务加载依赖DLL有特定路径要求:
- 32位SSIS运行时(VS2019默认):将所有依赖DLL复制到
C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn - 64位运行时:复制到
C:\Program Files\Microsoft SQL Server\150\DTS\Binn
确保所有依赖DLL都放在对应运行时的Binn目录,而非仅脚本任务项目目录。
2. 补全GAC注册
除Microsoft.Data.SqlClient.dll外,核心依赖(如Azure.Identity.dll、Microsoft.Identity.Client.dll)也需注册到GAC避免版本冲突:
- 以管理员身份运行命令提示符,执行
gacutil /i "DLL完整路径"注册核心DLL - 用
gacutil /l "DLL名称"验证注册是否成功
3. 添加绑定重定向配置
SSIS运行时可能加载旧版本依赖导致冲突,需在SSIS项目中添加绑定重定向:
- 右键SSIS项目 → 添加 → 新建项 → 选择“应用程序配置文件”,创建
App.config - 在
App.config中添加以下内容(根据实际DLL版本调整):
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Data.SqlClient" publicKeyToken="23ec7fc2d6eaa4a5" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Azure.Identity" publicKeyToken="92742159e12e44c8" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.6.0.0" newVersion="1.6.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Identity.Client" publicKeyToken="0a613f4dd989e8ae" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.45.0.0" newVersion="4.45.0.0" /> </dependentAssembly> <!-- 其他依赖的绑定重定向可参照格式添加 --> </assemblyBinding> </runtime> </configuration>
4. 调试DLL加载情况
在脚本任务中添加日志输出,检查DLL加载状态:
using System.Reflection; public void Main() { bool fireAgain = true; foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies()) { Dts.Events.FireInformation(0, "Loaded Assembly", $"Name: {assem.FullName}, Location: {assem.Location}", string.Empty, 0, ref fireAgain); } // 原有代码... }
运行包后查看日志,确认所有依赖DLL均被正确加载且版本匹配。
5. 降级SqlClient版本
SqlClient v5.0.0对.NET Framework 4.7的兼容性可能存在问题,尝试降级到v4.1.0或更低版本:
- 卸载当前SqlClient 5.0.0,安装v4.1.0,重新注册到GAC并更新依赖DLL
内容的提问来源于stack exchange,提问作者user18963348
相关产品推荐
相关产品推荐

