June 19, 2011 by Christoff Truter C# SQL
If you ever tried calling a webservice (among other things) from a SQLCLR procedure chances are pretty good that you've ran into the
following exception:
Msg 6522, Level 16, State 1, Procedure Test, Line 0 A .NET Framework error occurred during execution of user-defined routine or aggregate "Test": 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.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters options, String[] sources)
CREATE ASSEMBLY CLRProcedures FROM 'C:\demos\CSTruter.com\CLRSQL\bin\Release\SourceAssembly.dll' WITH PERMISSION_SET = UNSAFE GO CREATE ASSEMBLY CLRSerializer FROM 'C:\demos\CSTruter.com\CLRSQL\bin\Release\SourceAssembly.XmlSerializers.dll' WITH PERMISSION_SET = UNSAFE GO CREATE PROCEDURE Test AS EXTERNAL NAME [CLRProcedures].[CSTruter.com.StoredProcedures].[Test]
[XmlRoot("tester")]
public class tester
{
[XmlAttribute("name")]
public string name
{
get;
set;
}
}
tester t = new tester();
t.name = "test";
using (StringWriter sw = new StringWriter())
{
XmlSerializer xs = new XmlSerializer(typeof(tester));
xs.Serialize(sw, t);
}
tester t = null;
using (StringReader sr = new StringReader("some xml string"))
{
XmlSerializer xs = new XmlSerializer(typeof(tester));
t = xs.Deserialize(sr) as tester;
}
[XmlRoot("tester")]
[XmlSerializerAssembly("SourceAssembly.XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
public class tester
{
sgen C:\demos\CSTruter.com\CLRSQL\bin\Release\SourceAssembly.dll
Software Engineer May 3, 2013 by RWP
Thanks for the post. This solved my problem perfectly!