“无法加载动态生成的序列化程序集。”在SQL Server 2008 R2中执行调用Web服务的clr函数时出错。
我在Visual Studio 2017中有一个数据库项目,其中包括一个调用Web服务的clr函数。
项目属性包括以下内容:
项目设置目标平台= SQL Server 2008
SQLCLR目标框架= .Net Framework 3.5
SQLCLR构建生成序列化程序集=开
发布数据库时,将通过部署后脚本在数据库中创建序列化程序集。 当我在Windows 10 PC上将数据库发布到SQL Server 2008R2实例时,执行clr函数会导致“无法加载动态生成的序列化程序集”错误,但是,当数据库发布到PC上的SQL2016实例时,它运行正常
发布部署脚本以注册序列化程序集:
CREATE ASSEMBLY [CifasEdit.XmlSerializers] FROM 'C:\tfs\CIFAS\Source\Database\Hub\CifasEdit\CifasEdit\bin\Release\CifasEdit.XmlSerializers.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO
完整错误在这里
A .NET Framework error occurred during execution of user-defined routine or aggregate "xxx":
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.IO.FileLoadException:
at System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection)
at System.Reflection.Assembly.Load(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence securityEvidence)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch(CompilerParameters options, String[] sources)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.
...
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.GetSerializersFromCache(XmlMapping[] mappings, Type type)
at System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type)
at System.Web.Services.Protocols.SoapClientType..ctor(Type type)
at System.Web.Services.Protocols.SoapHttpClientProtocol..ctor()
答案 0 :(得分:1)
因此,尽管VS2017自动生成的序列化程序集在SQL2016上可以正常运行,但它似乎不适用于SQL Server 2008 R2。
要使CLR功能在2008R2中正常工作,我将CLR代码移到了一个单独的项目中,并使用sgen.exe手动生成了序列化程序集,然后注册了两个dll。
请确保对clr2.0使用sgen版本,在我的PC上,该版本位于
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin
生成序列化程序集的命令是
sgen.exe c:\YourDir\YourAssembly.dll
这将创建一个名为YourAssembly.XmlSerializers.dll的程序集 向SQL Server注册两个程序集:
CREATE ASSEMBLY [YourAssembly] FROM 'c:\YourDir\YourAssembly.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS
CREATE ASSEMBLY [YourAssembly.XmlSerializers] FROM 'c:\YourDir\YourAssembly.XmlSerializers.dll' WITH PERMISSION_SET = SAFE