SQL Server 2017 中引用其他程序集的 CLR 程序集注册期间出错

Error during the registration of a CLR assembly in SQL Server 2017 which references other assembly

当我尝试部署引用其他 ThirdPartyDLL 的 DLL 时出现以下错误。

Msg 10300, Level 16, State 2, Line 4
Assembly 'MYDLL' references assembly 'ThirdPartyDLL, version=300.1.0.1, culture=neutral, publickeytoken=5c915cbb2b8fbc32.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: version, culture or public key mismatch). Please load the referenced assembly into the current database and retry your request.

我使用这个声明来部署我的 dll:

USE MYDATABASE
GO

CREATE ASSEMBLY MYDLL
FROM 'C:\Program Files (x86)\MYDLL\MYDLL.dll'
WITH PERMISSION_SET = UNSAFE;
Go

我确定 ThirdPartyDLL 已正确安装,但我得到的错误是基于不匹配!!! 例如这个结果:

SELECT *
FROM sys.assemblies AS a
    INNER JOIN sys.assembly_files AS f ON a.assembly_id = f.assembly_id
FOR JSON PATH

是:

注意事项:

我能够在以下版本的 SQL 服务器上注册 MYDLL:

Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86) Jun 17 2011 00:57:23 Copyright (c) Microsoft Corporation Standard Edition on Windows NT 6.1 (Build 7601: Service Pack 1) (WOW64) (Hypervisor)

确保在构建 MYDLL 时将其设置为 x86 以匹配 ThirdPartyDLL 处理器架构。我认为它设置为 any 或 x64,因此它无法识别 x86 dll。似乎 MYDLL 在构建时设置为任何 CPU 或 x64,它无法识别 ThirdPartyDLL

的 x86 版本
  1. 转到“构建”>“配置管理器”菜单项。
  2. 在列表中找到您的项目,在平台下它会显示“任何 CPU”
  3. Select 下拉菜单中的“任何 CPU”选项,然后是 select
  4. 在该对话框中,select x86 来自“新平台”下拉菜单,并确保“任何 CPU”在“复制设置自”下拉菜单中 selected . 5.Hit 好的

注意:对于调试和发布配置,您需要 select x86。

我找到了这个问题的疯狂原因。基于以下主题:

SQL Server "version, culture or public key mismatch" during "create assembly" when loading XMLSerializers created with sgen utility

https://docs.microsoft.com/en-us/answers/questions/77832/i-got-the-following-error-when-i-try-to-deploy-my.html?sort=votes

我在 64 位服务器上引用了一个 x86 程序集。 SQL 服务器无法解析引用(基于不匹配的体系结构)和安装程序集。这意味着您可以在 SQL Server 2017 上安装任何 DLL (x86/x64),但前提是没有任何引用。 当有引用的dll时,它必须通过SQL服务器架构,才能找到它。

注意事项:

可以使用 MS CorFlags.exe.

将 dll 体系结构从 x64(64 位)更改为 x86(32 位)

https://docs.microsoft.com/en-us/dotnet/framework/tools/corflags-exe-corflags-conversion-tool