none
XmlSerializer. Cannot load dynamically generated serialization assembly

    Question

  • Hi

    I have a class in a VS 2010 project.

    I have added this project as a reference to another project. Here I set some of the class' properties and call functions in the class that serialize and deserialize the data using XmlSerializer.

    This works fine.

    However I want to also perform these tasks from a CLR function that is called by a SQL stored procedure.

    I have added a reference to my class into the CLR project.

    I have tried to follow the steps here to register the assembly - inc the XmlSerializer.dll

    However when I call the CLR function from a stored procedure it still returns "Cannot load dynamically generated serialization assembly"

    Admittedly I have found the steps in the link difficult to follow - for example the C# elements and for Visual Studio 2005 (I had to change the paths mentioned for sgen.exe). I'm also unsure whether I should follow method 1 or 2 based on my scenario.

    Does anyone have some simpler steps or guidance that may help me?

    Here's an extract from my class inc the serialize and deserialize functions.

    Imports System.Xml.Serialization
    Imports System.Xml
    Imports System.Text
    Imports System.IO
    
    <Serializable()> _
    Public Class clsExHeader
        Private mstrACCode As String
        Public Property ACCode() As String
            Get
                Return mstrACCode
            End Get
            Set(ByVal value As String)
                mstrACCode = value
            End Set
        End Property
        Private mstrExTransDate As String
        Public Property ExTransDate() As String
            Get
                Return mstrExTransDate
            End Get
            Set(ByVal value As String)
                mstrExTransDate = value
            End Set
        End Property
        Public Function Serialize() As String
            Try
                Dim _settings As XmlWriterSettings = New XmlWriterSettings
                Dim xml As StringBuilder = New StringBuilder
                Dim xw As XmlWriter = XmlWriter.Create(xml, _settings)
                Dim _serialize As New XmlSerializer(GetType(clsExHeader))
    
                _serialize.Serialize(xw, Me)
                Return xml.ToString
            Catch ex As Exception
                Throw (ex)
            End Try
    
        End Function
        Public Function Deserialize(ByVal strIn As String) As clsExHeader
            Try
                Dim _THlocal As New clsExHeader
                Dim _xrOut As New XmlTextReader(New StringReader(strIn))
                Dim _serialize As New XmlSerializer(GetType(clsExHeader))
    
                _THlocal = _serialize.Deserialize(_xrOut)
                Return _THlocal
            Catch ex As Exception
                Throw (ex)
            End Try
    
        End Function
    
    End Class

    Here's my CLR function

        Public Shared Sub exspXMLExTransactionAdd( _
            <Out()> <SqlFacet(MaxSize:=-1)> ByRef SQLXMLOut As SqlString _
            , <SqlFacet(MaxSize:=-1)> ByVal SQLXMLIn As SqlString _
            )
            Try
                Dim strXMLDataOut As String = ""
                'Deserialize the XML stream
                Dim _TH As New clsExHeader
                _TH.Deserialize(SQLXMLIn)
                'Change one of the value
                _TH.ExTransDate = "20121231"
                'Serialize this back
                strXMLDataOut = _TH.Serialize()
                SQLXMLOut = strXMLDataOut
            Catch ex As Exception
                Debug.Write(ex.Message)
                Throw New Exception("exspXMLExTransactionAdd:" & ex.Message & ex.StackTrace)
            End Try
            Return
        End Sub


    • Edited by bob132 Wednesday, April 03, 2013 5:24 PM
    • Moved by Shanks Zen Friday, April 05, 2013 5:05 AM From Visual Basic
    Wednesday, April 03, 2013 4:36 PM

Answers

  • The problem was because I needed to compile the XmlSerializer.dll as x86. I achieved this by including the required argument in the sgen.exe command.

    "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sgen.exe" /c:/platform:x86 /force "$(TargetPath)"

    • Marked as answer by bob132 Friday, May 03, 2013 9:15 AM
    Friday, May 03, 2013 9:15 AM

All replies

  • Hi Bob,

    We have a forum discuss processing XML issue, and I'll move this thread.

    Thanks,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 05, 2013 5:05 AM
  • The problem was because I needed to compile the XmlSerializer.dll as x86. I achieved this by including the required argument in the sgen.exe command.

    "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sgen.exe" /c:/platform:x86 /force "$(TargetPath)"

    • Marked as answer by bob132 Friday, May 03, 2013 9:15 AM
    Friday, May 03, 2013 9:15 AM