问题
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