XML Serialization Issue - by Ganesh Muthuvelu

Status : 

  Postponed<br /><br />
		Due to current priorities, the product team decided to postpone the resolution of this item.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 209210 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 9/22/2006 11:29:38 AM
Access Restriction Public


A .NET assembly that generates a XML schema at run time cannot be called from SQL Server 2005.  Please note that the assembly has been "sgen" and both assemblies are loaded into SQL Server with "SAFE" permission.

Below is the error message:
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. 
Sign in to post a comment.
Posted by Microsoft on 10/5/2006 at 10:55 AM
Steven Hemingray already followed up with the problem offline in email, and I am just posting the findings here.

The issue is in method GetSchemaString, which calls XmlSchema.Write:
Public Function GetSchemaString(ByVal GeneratedSchema As XmlSchema, ByRef ErrMsg As String) As String
        Dim XMLSchemaStr As String
        Dim memStream As New MemoryStream
        Dim Writer As XmlTextWriter
            Writer = New XmlTextWriter(memStream, New UTF8Encoding)
            Writer.Formatting = Formatting.Indented

XmlSchema.Write however calls
XmlSerializer serializer = new XmlSerializer(typeof(XmlSchema));

This causes Serialization.TempAssembly.LoadGeneratedAssembly to try to load the System.Xml.XmlSerializers assembly. This assembly is not found, so it tries to create a dynamic assembly which fails under SQL CLR.

There is a workaround for the issue, however it is pretty messy. Here are the steps for this case:
1.     Run sgen on System.Xml.dll file to generate an XmlSerializer for the XmlSchema class, like so:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>sgen /t:System.Xml.Schema.XmlSchema System.Xml.Dll
2.    This will output an error message saying the assembly cannot be signed with the correct key, however a System.Xml.XmlSerializers.dll.DELETED file will be created. Rename this file to System.Xml.XmlSerializers.Dll and add it as a reference in the project. Add the following to your declaration
Imports Microsoft.Xml.Serialization.GeneratedAssembly
3.    Replace this code in GetSchemaString:


Dim GeneratedSchemaSerializer As XmlSchemaSerializer
            GeneratedSchemaSerializer = New XmlSchemaSerializer()
            GeneratedSchemaSerializer.Serialize(Writer, GeneratedSchema)

Best regards,