SQLCLR Assembly deployment failed because assembly failed verification

怎甘沉沦 提交于 2019-12-11 00:33:45

问题


In case someone else runs into this problem.

Error: SQL72014: .Net SqlClient Data Provider: ... CREATE ASSEMBLY for assembly 'Assembly.Name' failed because assembly 'Assembly.Name' 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
[token 0x02000003] Type load failed.

The SQLCLR assembly builds successfully but couldn't be deployed. When the assembly is accessed by regular a .NET application (outside of SQL Server), it will give a TypeLoadException:

Could not load type 'Type.In.Assembly' from assembly 'Assembly.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the format is invalid.

The problem is related to the User-Defined Type (UDT) within the assembly.


回答1:


The culprit is

System.Runtime.InteropServices.StructLayoutAttribute

When we define a UDT using
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.Native)] and the UDT is a class (instead of a struct) it is required to also define a [StructLayout(LayoutKind.Sequential)] on the class.

The problem was my UDT class was inheriting from a base class. The StructLayout must be defined on the base class too.



来源:https://stackoverflow.com/questions/39741896/sqlclr-assembly-deployment-failed-because-assembly-failed-verification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!