无法加载动态生成的序列化程序集 - SQL Server 2008 R2 CLR

Cannot load dynamically generated serialization assembly - SQL Server 2008 R2 CLR

我遇到了 运行 存储过程的问题,这些存储过程在我构建并经过完全单元测试的自定义 .Net 程序集中执行方法。在 SQL Server 2014 中,我的解决方案有效并且存储过程执行得很好,只有当我使用 SQL Server 2008 R2 时才会出现这些错误。

问题是当我将我的程序集添加到 SQL Server 2008 R2,创建我的存储过程,并像这样执行其中之一时:

 EXEC dbo.Avatas_CallAddCustomerAndAddCreditCardService 4111111111111111, '1025' , 925 , 
      3500, '20/20' , 'asdf' , 1111, 1111 , '600094' , '0442375110' , 
      'asdf' , 'asdf' , '01' , NULL , 7777

我收到以下错误:

Msg 6522, Level 16, State 1, ProcedureAvatas_CallAddCustomerAndAddCreditCardService, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "Avatas_CallAddCustomerAndAddCreditCardService": System.InvalidOperationException: Cannot load dynamically generated serialization assembly. In some hosting environments assembly load functionality is restricted, consider using pre-generated serializer. Please see inner exception for more information.

System.IO.FileLoadException: LoadFrom(), LoadFile(), Load(byte[]) and LoadModule() have been disabled by the host.

System.InvalidOperationException:
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace, String location, Evidence evidence) at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlRootAttribute root) at FirestreamAvatasCustomerVault.AscendAvatasInt35.AvatasWebServiceCaller.DeserializeAvatasResponse(String response) at FirestreamAvatasCustomerVault.AscendAvatasInt35.AvatasWebServiceCaller.CallManageCustomerService(Int32 personId, String address1, String address2, Int32 cityId, Int32 stateProvId, String zip, String phone, String firstName, String lastName, String company, Nullable1 customerVaultId, Nullable1 clientId) at FirestreamAvatasCustomerVault.AscendAvatasInt35.AvatasWebServiceCaller.CallAddCusto...

我已将“生成序列化程序集”更改为“打开”,并注册了它生成的 XMLSerializers.dll 以及实际的 dll。

这是我创建的存储过程和程序集:

CREATE ASSEMBLY [Avatas]
AUTHORIZATION [dbo]
FROM 'D:\TFS Workspace\Firestream\Dev\AvatasCustomerVault\FirestreamAvatasCustomerVault\FirestreamAvatasCustomerVault.AscendAvatasInt35\bin\.Net 3.5'
WITH PERMISSION_SET = UNSAFE
CREATE ASSEMBLY [Avatas.XmlSerializers]
AUTHORIZATION [dbo]
FROM 'D:\TFS Workspace\Firestream\Dev\AvatasCustomerVault\FirestreamAvatasCustomerVault\FirestreamAvatasCustomerVault.AscendAvatasInt35\bin\.Net 3.5\FirestreamAvatasCustomerVault.AscendAvatasInt35.XmlSerializers.dll'
WITH PERMISSION_SET = UNSAFE

exec('CREATE PROCEDURE [dbo].[Avatas_CallAddCustomerAndAddCreditCardService](@creditCardNumber bigint, @expirationDate nvarchar(10), @ccv int, 
         @personId int, @address1 nvarchar(100), @address2 nvarchar(100), 
@cityId int,
         @stateProvId int, @zip nvarchar(50), @phone nvarchar(20), @firstName nvarchar(150), @lastName nvarchar(150), @company nvarchar(150), @customerVaultId int, @clientId int)
AS EXTERNAL NAME [Avatas].[FirestreamAvatasCustomerVault.AscendAvatasInt35.AvatasWebServiceCaller].[CallAddCustomerAndAddCreditCardService]')

实际发生错误的行如下:

XmlSerializer serializer = new XmlSerializer(typeof(AvatasResponse), xRoot);

我找到的最终解决方案是 SQL Server 2008 和 2008 R2 仅支持编译到 .Net Framework 3.5 版的 dll。而 SQL Server 2012+ 仅支持版本 4 及以上。