繁体   English   中英

SQL Server 自定义 CLR 失败并显示错误“无法加载文件或程序集或其依赖项之一。系统找不到指定的文件。”

[英]SQL Server custom CLR fails with error "Could not load file or assembly or one of its dependencies. The system cannot find the file specified."

我创建了一个引用 Windows DLL WindowsBase 的 SQL CLR。 在大多数情况下,我的 CLR 工作得很好,但是如果 WindowsBase 在 GAC 中得到更新,那么我会收到错误消息“ Assembly in host store has a different signature than assembly in GAC. ”。

为了解决这个问题,我构建了我的 CLR 以引用不在 GAC 中的 WindowsBase 版本。 现在,当我运行 CLR 时,出现错误“ Could not load file or assembly 'WindowsBase, Version=4.0.0.0, ...' or one of its dependencies. The system cannot find the file specified.

我设置了 FusLogVw 来查看发生了什么并得到以下输出

The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
    LOG: DisplayName = WindowsBase, Version=4.0.0.0
    LOG: Appbase = file:///C:/Program Files/Microsoft SQL Server/MSSQL11.MSSQLSERVER/MSSQL/Binn/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = sqlservr.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: WindowsBase, Version=4.0.0.0
    LOG: The same bind was seen before, and was failed with hr = 0x80070002.
    ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).

看起来程序集加载器正在使用 machine.config 文件来确定它应该在哪里查找程序集。 基于这个假设,我更新了 machine.config

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
             <assemblyIdentity name="WindowsBase"... />
              <codBase version="4.0.0.0"
                   href="./AdditionalAssemblies/WindowsBase.dll"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

我已尝试使用相对路径(相对于 SQlservr.exe)和绝对路径,但继续遇到上述相同的错误。 关于如何设置 SQL CLR 以便它引用 GAC 之外的 WindowsBase 副本而不会出现上述错误的任何建议将不胜感激。

~Bugz~

SQL Server 的 CLR 主机不支持您尝试执行的操作。 SQL Server 中的 CLR 受到严格限制,以防止破坏 SQL Server 的稳定性,因为它的工作方式与在操作系统上运行的应用程序不同。 因此,支持的 DLL 集非常有限(即经过验证可以工作并保证在更新到 .NET 后仍能正常工作)。 WindowsBase 不是其中之一,因此您需要将其作为UNSAFE手动加载到 SQL Server 中。 但这会让您遇到主 GAC 中版本更改的问题(GAC 和 SQL Server 的 CLR 主机之间常见的 DLL 必须是相同版本),或者更糟的是,如果 DLL 变得“混合” (非托管 C++ 和托管代码)并且不再“纯”。 在这种情况下,新版本不会加载,旧版本会出现“错误版本”错误,因此您需要做一些工作。

有关更多详细信息,请参阅以下文章/文档:

这组链接取自我写的文章的“附加阅读”部分: 通往 SQLCLR 级别 5:开发(在 SQL Server 中使用 .NET)的阶梯


有关使用 SQLCLR 的更多信息,请访问我的网站: SQLCLR 信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM