将现有数据库 (SQL2012) 导入到 VS2017 中的 SSDT 项目,并在尝试将其部署到我的本地实例 (SQL2016) 时收到错误消息:
为程序集“System.DirectoryServices”创建程序集失败,因为程序集“System.DirectoryServices”验证失败。
将它指向以下内容时,我可以在 SSMS 的数据库中创建程序集:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.DirectoryServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.DirectoryServices.dll
在我的 SSDT 项目中,我将目标框架设置为 SQLCLR 下的 .NET Framework 4。
对 System.DirectoryServices 的引用已将 Generate Sql Script 和 Model Aware 选项设置为 true,并将权限设置为 Unsafe。
数据库设置还启用了可信任选项。
select * from sys.dm_clr_properties
在我的实例上运行返回:
directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
version v4.0.30319
state CLR is initialized
我需要更改什么才能使我的项目成功发布?
完整的错误信息如下。
Creating [System.DirectoryServices]...
Warning: The Microsoft .NET Framework assembly 'system.directoryservices, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.' you are registering is not fully tested in the SQL Server hosted environment and is not supported. In the future, if you upgrade or service this assembly or the .NET Framework, your CLR integration routine may stop working. Please refer SQL Server Books Online for more details.
(221,1): SQL72014: .Net SqlClient Data Provider: Msg 6218, Level 16, State 2, Line 1 CREATE ASSEMBLY for assembly 'System.DirectoryServices' failed because assembly 'System.DirectoryServices' failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database. CLR Verifier error messages if any will follow this message
[ : System.DirectoryServices.ActiveDirectorySecurity::PurgeAccessRules][mdToken=0x6000009][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::PurgeAuditRules][mdToken=0x6000010][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::get_AccessRightType][mdToken=0x6000015][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::get_AccessRuleType][mdToken=0x6000016][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::get_AuditRuleType][mdToken=0x6000017][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::ModifyAccessRule][mdToken=0x6000008][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::ModifyAuditRule][mdToken=0x600000f][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::AccessRuleFactory][mdToken=0x6000011][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::AuditRuleFactory][mdToken=0x6000013][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::AccessRuleFactory][mdToken=0x6000012][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::AuditRuleFactory][mdToken=0x6000014][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::.ctor][mdToken=0x6000001][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurity::AddAccessRule][mdToken=0x6000002][offset 0x00000000] Code size is zero.
[ : System.DirectoryServices.ActiveDirectorySecurit...
(221,0): SQL72045: Script execution error. The executed script:
CREATE ASSEMBLY [System.DirectoryServices]
AUTHORIZATION [dbo]
FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C010300A1E3A14B0000000000000000E00022200B01080000E801000008000000000000C205020000200000002002000000AC65002000000002000004000000000000000400000000000000006002000002000062580200030040850000100000100000000010000010000000000000100000000000000000000000700502004F000000002002006C040000000000000000000000F2010070170000004002000C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E7465787400000038E601000020000000E8010000020000000000000000000000000000200000602E727372630000006C040000002002000006000000EA0100000000000000000000000000400000402E72656C6F6300000C00000000400200
An error occurred while the batch was being executed.
我不确定这种行为是 SSDT 有意为之还是对先前版本的更改,但正在发生的是“参考程序集”被拉入。参考程序集不是实际程序集,因此它们不会起作用导入 SQL Server。这里令人沮丧的是,即使您在添加引用时使用“浏览”并选择C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\System.DirectoryServices.dll然后
Model Aware
在其属性下选择“true”, SSDT 仍将引入参考程序集(114 kb)而不是您指向的 DLL(413 kb)。更具体地说,它将从与项目属性中设置的“目标框架版本”对应的文件夹中拉入参考程序集(例如 4.6.1 ==C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1)。如果重要的话,我正在使用 Visual Studio 2015 和 SSDT for Management Studio 2017(即 v14)。然而,虽然不理想,但我能够通过将 DLL 复制到非系统文件夹来使其工作。我将C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\中的完整System.DirectoryServices.dll复制到我的项目文件夹中,然后我添加了一个“现有项”以指向 DLL(在我的项目文件夹中) ,它有一个“构建操作” ,然后将 Reference 属性设置为:Owner = 、Permission Set =和 Model Aware = 。之后它起作用了。
None
dbo
UNSAFE
True
如果这个 DLL 有任何依赖项,那么我想我也需要将它们复制到我的项目中。但我不是 100% 确定那部分。
我想我们可以将此经验添加到不使用不受支持的 .NET Framework 库和/或 SSDT 的原因列表中;-)