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

问题描述

当我尝试部署引用其他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.

我使用此statemant来部署我的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

是:

DLL ERROR

通知:

我能够在以下版本的SQL Server上注册MYDLL:

Microsoft SQL Server 2008 R2(SP1)-10.50.2500.0(Intel X86)6月17日 2011 00:57:23版权所有(c)Microsoft Corporation标准版 在Windows NT 6.1(内部版本7601:Service Pack 1)(WOW64)上 (管理程序)

解决方法

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

  1. 转到Build> Configuration Manager菜单项。
  2. 在列表中找到您的项目,在“平台”下它将显示“ Any CPU”
  3. 从下拉菜单中选择“任何CPU”选项,然后选择
  4. 从该对话框中,从“新平台”(New Platform)下拉列表中选择x86,并确保在“复制设置自”(Copy settings from)下拉列表中选择了“任何CPU”。 5.点击确定

注意:您将要同时为调试和发布配置选择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 Server无法解析引用(基于不匹配的体系结构)并安装程序集。这意味着您可以在没有任何引用的情况下在SQL Server 2017上安装任何DLL(x86 / x64)。 当存在引用的dll时,它必须通过SQL Server体系结构,以便可以找到它。

通知:

可以使用MS CorFlags.exe将dll体系结构从x64(64位)更改为x86(32位)。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...